globals.pas 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements some support functions and global variables
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit globals;
  19. {$i fpcdefs.inc}
  20. { Use the internal linker by default }
  21. { define INTERNALLINKER}
  22. interface
  23. uses
  24. {$ifdef win32}
  25. windows,
  26. {$endif}
  27. {$ifdef hasunix}
  28. {$ifdef havelinuxrtl10}
  29. linux,
  30. {$else}
  31. Baseunix,unix,
  32. {$endif}
  33. {$endif}
  34. {$ifdef Delphi}
  35. SysUtils,
  36. dmisc,
  37. {$else}
  38. strings,
  39. dos,
  40. {$endif}
  41. cutils,cclasses,
  42. globtype,version,systems,cpuinfo;
  43. const
  44. {$ifdef Splitheap}
  45. testsplit : boolean = false;
  46. {$endif Splitheap}
  47. delphimodeswitches : tmodeswitches=
  48. [m_delphi,m_all,m_class,m_objpas,m_result,m_string_pchar,
  49. m_pointer_2_procedure,m_autoderef,m_tp_procvar,m_initfinal,m_default_ansistring,
  50. m_out,m_default_para,m_duplicate_names,m_hintdirective,m_add_pointer];
  51. fpcmodeswitches : tmodeswitches=
  52. [m_fpc,m_all,m_string_pchar,m_nested_comment,m_repeat_forward,
  53. m_cvar_support,m_initfinal,m_add_pointer,m_hintdirective];
  54. objfpcmodeswitches : tmodeswitches=
  55. [m_objfpc,m_fpc,m_all,m_class,m_objpas,m_result,m_string_pchar,m_nested_comment,
  56. m_repeat_forward,m_cvar_support,m_initfinal,m_add_pointer,m_out,m_default_para,m_hintdirective];
  57. tpmodeswitches : tmodeswitches=
  58. [m_tp7,m_all,m_tp_procvar,m_duplicate_names];
  59. gpcmodeswitches : tmodeswitches=
  60. [m_gpc,m_all];
  61. macmodeswitches : tmodeswitches=
  62. [m_mac,m_all,m_result,m_repeat_forward,m_cvar_support];
  63. { maximum number of locals in bytes before warning is emitted }
  64. maxlocalsize = high(smallint);
  65. { maximum number of paras in bytes before warning is emitted }
  66. maxparasize = high(word);
  67. { maximum nesting of routines }
  68. maxnesting = 32;
  69. treelogfilename = 'tree.log';
  70. { I don't know if this endian dependend }
  71. MathQNaN : array[0..7] of byte = (0,0,0,0,0,0,252,255);
  72. MathInf : array[0..7] of byte = (0,0,0,0,0,0,240,127);
  73. MathNegInf : array[0..7] of byte = (0,0,0,0,0,0,240,255);
  74. type
  75. TFPUException = (exInvalidOp, exDenormalized, exZeroDivide,
  76. exOverflow, exUnderflow, exPrecision);
  77. TFPUExceptionMask = set of TFPUException;
  78. pfileposinfo = ^tfileposinfo;
  79. tfileposinfo = record
  80. line : longint;
  81. column : word;
  82. fileindex : word;
  83. { moduleindex : word; }
  84. end;
  85. TSearchPathList = class(TStringList)
  86. procedure AddPath(s:string;addfirst:boolean);overload;
  87. procedure AddPath(SrcPath,s:string;addfirst:boolean);overload;
  88. procedure AddList(list:TSearchPathList;addfirst:boolean);
  89. function FindFile(const f : string;var foundfile:string):boolean;
  90. end;
  91. tcodepagestring = string[20];
  92. var
  93. { specified inputfile }
  94. inputdir : dirstr;
  95. inputfile : namestr;
  96. inputextension : extstr;
  97. { specified outputfile with -o parameter }
  98. outputfile : namestr;
  99. outputprefix : pstring;
  100. outputsuffix : pstring;
  101. outputextension : namestr;
  102. { specified with -FE or -FU }
  103. outputexedir : dirstr;
  104. outputunitdir : dirstr;
  105. { things specified with parameters }
  106. paralinkoptions,
  107. paradynamiclinker : string;
  108. paraprintnodetree : byte;
  109. parapreprocess : boolean;
  110. printnodefile : text;
  111. { typical cross compiling params}
  112. { directory where the utils can be found (options -FD) }
  113. utilsdirectory : dirstr;
  114. { targetname specific prefix used by these utils (options -XP<path>) }
  115. utilsprefix : dirstr;
  116. cshared : boolean; { pass --shared to ld to link C libs shared}
  117. Dontlinkstdlibpath: Boolean; { Don't add std paths to linkpath}
  118. rlinkpath : dirstr; { rpath-link linkdir override}
  119. { some flags for global compiler switches }
  120. do_build,
  121. do_release,
  122. do_make : boolean;
  123. { path for searching units, different paths can be seperated by ; }
  124. exepath : dirstr; { Path to ppc }
  125. librarysearchpath,
  126. unitsearchpath,
  127. objectsearchpath,
  128. includesearchpath : TSearchPathList;
  129. { linking }
  130. usewindowapi : boolean;
  131. description : string;
  132. DescriptionSetExplicity : boolean;
  133. dllversion : string;
  134. dllmajor,
  135. dllminor,
  136. dllrevision : word; { revision only for netware }
  137. UseDeffileForExports : boolean;
  138. UseDeffileForExportsSetExplicitly : boolean;
  139. RelocSection : boolean;
  140. RelocSectionSetExplicitly : boolean;
  141. LinkTypeSetExplicitly : boolean;
  142. akttokenpos, { position of the last token }
  143. aktfilepos : tfileposinfo; { current position }
  144. nwscreenname : string;
  145. nwthreadname : string;
  146. nwcopyright : string;
  147. codegenerror : boolean; { true if there is an error reported }
  148. block_type : tblock_type; { type of currently parsed block }
  149. parsing_para_level : integer; { parameter level, used to convert
  150. proc calls to proc loads in firstcalln }
  151. compile_level : word;
  152. make_ref : boolean;
  153. resolving_forward : boolean; { used to add forward reference as second ref }
  154. inlining_procedure : boolean; { are we inlining a procedure }
  155. exceptblockcounter : integer; { each except block gets a unique number check gotos }
  156. aktexceptblock : integer; { the exceptblock number of the current block (0 if none) }
  157. { commandline values }
  158. initdefines : tstringlist;
  159. initglobalswitches : tglobalswitches;
  160. initmoduleswitches : tmoduleswitches;
  161. initlocalswitches : tlocalswitches;
  162. initmodeswitches : tmodeswitches;
  163. {$IFDEF testvarsets}
  164. Initsetalloc, {0=fixed, 1 =var}
  165. {$ENDIF}
  166. initpackenum : shortint;
  167. {$ifdef ansistring_bits}
  168. initansistring_bits: Tstringbits;
  169. {$endif}
  170. initalignment : talignmentinfo;
  171. initoptprocessor,
  172. initspecificoptprocessor : tprocessors;
  173. initfputype : tfputype;
  174. initasmmode : tasmmode;
  175. initinterfacetype : tinterfacetypes;
  176. initoutputformat : tasm;
  177. initdefproccall : tproccalloption;
  178. initsourcecodepage : tcodepagestring;
  179. { current state values }
  180. aktglobalswitches : tglobalswitches;
  181. aktmoduleswitches : tmoduleswitches;
  182. aktlocalswitches : tlocalswitches;
  183. nextaktlocalswitches : tlocalswitches;
  184. localswitcheschanged : boolean;
  185. aktmodeswitches : tmodeswitches;
  186. {$IFDEF testvarsets}
  187. aktsetalloc,
  188. {$ENDIF}
  189. aktpackrecords,
  190. aktpackenum : longint;
  191. {$ifdef ansistring_bits}
  192. aktansistring_bits : Tstringbits;
  193. {$endif}
  194. aktmaxfpuregisters : longint;
  195. aktalignment : talignmentinfo;
  196. aktoptprocessor,
  197. aktspecificoptprocessor : tprocessors;
  198. aktfputype : tfputype;
  199. aktasmmode : tasmmode;
  200. aktinterfacetype : tinterfacetypes;
  201. aktoutputformat : tasm;
  202. aktdefproccall : tproccalloption;
  203. aktsourcecodepage : tcodepagestring;
  204. { Memory sizes }
  205. heapsize,
  206. stacksize : 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 : pstring = 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. {$ifdef m68k}
  227. { PalmOS resources }
  228. palmos_applicationname : string = 'FPC Application';
  229. palmos_applicationid : string[4] = 'FPCA';
  230. {$endif m68k}
  231. procedure abstract;
  232. function bstoslash(const s : string) : string;
  233. function getdatestr:string;
  234. function gettimestr:string;
  235. function filetimestring( t : longint) : string;
  236. procedure DefaultReplacements(var s:string);
  237. function GetCurrentDir:string;
  238. function path_absolute(const s : string) : boolean;
  239. Function PathExists ( F : String) : Boolean;
  240. Function FileExists ( Const F : String) : Boolean;
  241. Function RemoveFile(const f:string):boolean;
  242. Function RemoveDir(d:string):boolean;
  243. Function GetFileTime ( Var F : File) : Longint;
  244. Function GetNamedFileTime ( Const F : String) : Longint;
  245. Function SplitPath(const s:string):string;
  246. Function SplitFileName(const s:string):string;
  247. Function SplitName(const s:string):string;
  248. Function SplitExtension(Const HStr:String):String;
  249. Function AddExtension(Const HStr,ext:String):String;
  250. Function ForceExtension(Const HStr,ext:String):String;
  251. Function FixPath(s:string;allowdot:boolean):string;
  252. function FixFileName(const s:string):string;
  253. function TargetFixPath(s:string;allowdot:boolean):string;
  254. function TargetFixFileName(const s:string):string;
  255. procedure SplitBinCmd(const s:string;var bstr,cstr:string);
  256. function FindFile(const f : string;path : string;var foundfile:string):boolean;
  257. function FindFilePchar(const f : string;path : pchar;var foundfile:string):boolean;
  258. function FindExe(const bin:string;var foundfile:string):boolean;
  259. function GetShortName(const n:string):string;
  260. function Shell(const command:string): longint;
  261. function GetEnvPChar(const envname:string):pchar;
  262. procedure FreeEnvPChar(p:pchar);
  263. function SetFPUExceptionMask(const Mask : TFPUExceptionMask) : TFPUExceptionMask;
  264. function is_number_float(d : double) : boolean;
  265. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  266. function SetAktProcCall(const s:string; changeInit: boolean):boolean;
  267. function SetProcessor(const s:string; changeInit: boolean):boolean;
  268. function SetFpuType(const s:string; changeInit: boolean):boolean;
  269. procedure InitGlobals;
  270. procedure DoneGlobals;
  271. function string2guid(const s: string; var GUID: TGUID): boolean;
  272. function guid2string(const GUID: TGUID): string;
  273. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  274. {# Routine to get the required alignment for size of data, which will
  275. be placed in bss segment, according to the current alignment requirements }
  276. function var_align(siz: longint): longint;
  277. {# Routine to get the required alignment for size of data, which will
  278. be placed in data/const segment, according to the current alignment requirements }
  279. function const_align(siz: longint): longint;
  280. implementation
  281. uses
  282. comphook;
  283. procedure abstract;
  284. begin
  285. do_internalerror(255);
  286. end;
  287. procedure WarnNonExistingPath(const path : string);
  288. begin
  289. if assigned({$ifndef FPCPROCVAR}@{$endif}do_comment) then
  290. do_comment(V_Hint,'Path "'+path+'" not found');
  291. end;
  292. function bstoslash(const s : string) : string;
  293. {
  294. return string s with all \ changed into /
  295. }
  296. var
  297. i : longint;
  298. begin
  299. for i:=1to length(s) do
  300. if s[i]='\' then
  301. bstoslash[i]:='/'
  302. else
  303. bstoslash[i]:=s[i];
  304. bstoslash[0]:=s[0];
  305. end;
  306. {****************************************************************************
  307. Time Handling
  308. ****************************************************************************}
  309. Function L0(l:longint):string;
  310. {
  311. return the string of value l, if l<10 then insert a zero, so
  312. the string is always at least 2 chars '01','02',etc
  313. }
  314. var
  315. s : string;
  316. begin
  317. Str(l,s);
  318. if l<10 then
  319. s:='0'+s;
  320. L0:=s;
  321. end;
  322. function gettimestr:string;
  323. {
  324. get the current time in a string HH:MM:SS
  325. }
  326. var
  327. hour,min,sec,hsec : word;
  328. begin
  329. {$ifdef delphi}
  330. dmisc.gettime(hour,min,sec,hsec);
  331. {$else delphi}
  332. dos.gettime(hour,min,sec,hsec);
  333. {$endif delphi}
  334. gettimestr:=L0(Hour)+':'+L0(min)+':'+L0(sec);
  335. end;
  336. function getdatestr:string;
  337. {
  338. get the current date in a string YY/MM/DD
  339. }
  340. var
  341. Year,Month,Day,Wday : Word;
  342. begin
  343. {$ifdef delphi}
  344. dmisc.getdate(year,month,day,wday);
  345. {$else}
  346. dos.getdate(year,month,day,wday);
  347. {$endif}
  348. getdatestr:=L0(Year)+'/'+L0(Month)+'/'+L0(Day);
  349. end;
  350. function filetimestring( t : longint) : string;
  351. {
  352. convert dos datetime t to a string YY/MM/DD HH:MM:SS
  353. }
  354. var
  355. DT : DateTime;
  356. begin
  357. if t=-1 then
  358. begin
  359. FileTimeString:='Not Found';
  360. exit;
  361. end;
  362. unpacktime(t,DT);
  363. filetimestring:=L0(dt.Year)+'/'+L0(dt.Month)+'/'+L0(dt.Day)+' '+L0(dt.Hour)+':'+L0(dt.min)+':'+L0(dt.sec);
  364. end;
  365. {****************************************************************************
  366. Default Macro Handling
  367. ****************************************************************************}
  368. procedure DefaultReplacements(var s:string);
  369. begin
  370. { Replace some macros }
  371. Replace(s,'$FPCVER',version_string);
  372. Replace(s,'$VERSION',version_string);
  373. Replace(s,'$FULLVERSION',full_version_string);
  374. Replace(s,'$FPCDATE',date_string);
  375. Replace(s,'$FPCTARGET',target_cpu_string);
  376. Replace(s,'$FPCCPU',target_cpu_string);
  377. Replace(s,'$TARGET',target_path);
  378. Replace(s,'$FPCOS',target_path);
  379. end;
  380. {****************************************************************************
  381. File Handling
  382. ****************************************************************************}
  383. function GetCurrentDir:string;
  384. var
  385. CurrentDir : string;
  386. begin
  387. GetDir(0,CurrentDir);
  388. GetCurrentDir:=FixPath(CurrentDir,false);
  389. end;
  390. function path_absolute(const s : string) : boolean;
  391. {
  392. is path s an absolute path?
  393. }
  394. begin
  395. path_absolute:=false;
  396. {$ifdef unix}
  397. if (length(s)>0) and (s[1]='/') then
  398. path_absolute:=true;
  399. {$else unix}
  400. {$ifdef amiga}
  401. if ((length(s)>0) and ((s[1]='\') or (s[1]='/'))) or (Pos(':',s) = length(s)) then
  402. path_absolute:=true;
  403. {$else}
  404. if ((length(s)>0) and ((s[1]='\') or (s[1]='/'))) or
  405. ((length(s)>2) and (s[2]=':') and ((s[3]='\') or (s[3]='/'))) then
  406. path_absolute:=true;
  407. {$endif amiga}
  408. {$endif unix}
  409. end;
  410. {$ifndef FPC}
  411. Procedure FindClose(var Info : SearchRec);
  412. Begin
  413. End;
  414. {$endif not FPC}
  415. Function FileExists ( Const F : String) : Boolean;
  416. Var
  417. res : boolean;
  418. {$ifndef delphi}
  419. Info : SearchRec;
  420. {$endif}
  421. begin
  422. {$ifdef delphi}
  423. res:=sysutils.FileExists(f);
  424. {$else}
  425. findfirst(F,readonly+archive+hidden,info);
  426. res:=(doserror=0);
  427. findclose(Info);
  428. {$endif delphi}
  429. if assigned({$ifndef FPCPROVCAR}@{$endif}do_comment) then
  430. begin
  431. if res then
  432. do_comment(V_Tried,'Searching file '+F+'... found')
  433. else
  434. do_comment(V_Tried,'Searching file '+F+'... not found');
  435. end;
  436. FileExists:=res;
  437. end;
  438. Function PathExists ( F : String) : Boolean;
  439. Var
  440. Info : SearchRec;
  441. disk : byte;
  442. begin
  443. { these operating systems have dos type drives }
  444. if source_info.system in [system_m68k_atari,system_i386_go32v2,
  445. system_i386_win32,system_i386_os2,
  446. system_i386_emx,system_i386_wdosx] then
  447. Begin
  448. if (Length(f)=3) and (F[2]=':') and (F[3] in ['/','\']) then
  449. begin
  450. if F[1] in ['A'..'Z'] then
  451. disk:=ord(F[1])-ord('A')+1
  452. else if F[1] in ['a'..'z'] then
  453. disk:=ord(F[1])-ord('a')+1
  454. else
  455. disk:=255;
  456. if disk=255 then
  457. PathExists:=false
  458. else
  459. PathExists:=(DiskSize(disk)<>-1);
  460. exit;
  461. end;
  462. end;
  463. if F[Length(f)] in ['/','\'] then
  464. Delete(f,length(f),1);
  465. findfirst(F,readonly+archive+hidden+directory,info);
  466. PathExists:=(doserror=0) and ((info.attr and directory)=directory);
  467. findclose(Info);
  468. end;
  469. Function RemoveFile(const f:string):boolean;
  470. var
  471. g : file;
  472. begin
  473. assign(g,f);
  474. {$I-}
  475. erase(g);
  476. {$I+}
  477. RemoveFile:=(ioresult=0);
  478. end;
  479. Function RemoveDir(d:string):boolean;
  480. begin
  481. if d[length(d)]=source_info.DirSep then
  482. Delete(d,length(d),1);
  483. {$I-}
  484. rmdir(d);
  485. {$I+}
  486. RemoveDir:=(ioresult=0);
  487. end;
  488. Function SplitPath(const s:string):string;
  489. var
  490. i : longint;
  491. begin
  492. i:=Length(s);
  493. while (i>0) and not(s[i] in ['/','\']) do
  494. dec(i);
  495. SplitPath:=Copy(s,1,i);
  496. end;
  497. Function SplitFileName(const s:string):string;
  498. var
  499. p : dirstr;
  500. n : namestr;
  501. e : extstr;
  502. begin
  503. FSplit(s,p,n,e);
  504. SplitFileName:=n+e;
  505. end;
  506. Function SplitName(const s:string):string;
  507. var
  508. i,j : longint;
  509. begin
  510. i:=Length(s);
  511. j:=Length(s);
  512. while (i>0) and not(s[i] in ['/','\']) do
  513. dec(i);
  514. while (j>0) and (s[j]<>'.') do
  515. dec(j);
  516. if j<=i then
  517. j:=255;
  518. SplitName:=Copy(s,i+1,j-(i+1));
  519. end;
  520. Function SplitExtension(Const HStr:String):String;
  521. var
  522. j : longint;
  523. begin
  524. j:=length(Hstr);
  525. while (j>0) and (Hstr[j]<>'.') do
  526. begin
  527. if hstr[j]=source_info.DirSep then
  528. j:=0
  529. else
  530. dec(j);
  531. end;
  532. if j=0 then
  533. j:=254;
  534. SplitExtension:=Copy(Hstr,j,255);
  535. end;
  536. Function AddExtension(Const HStr,ext:String):String;
  537. begin
  538. if (Ext<>'') and (SplitExtension(HStr)='') then
  539. AddExtension:=Hstr+Ext
  540. else
  541. AddExtension:=Hstr;
  542. end;
  543. Function ForceExtension(Const HStr,ext:String):String;
  544. var
  545. j : longint;
  546. begin
  547. j:=length(Hstr);
  548. while (j>0) and (Hstr[j]<>'.') do
  549. dec(j);
  550. if j=0 then
  551. j:=255;
  552. ForceExtension:=Copy(Hstr,1,j-1)+Ext;
  553. end;
  554. {Translates a unix or dos path to a mac path for use in MPW.
  555. If already a mac path, it does nothing. The origin of this
  556. algorithm will be put in macos/dos.pp, please update this
  557. from that, because there is some flaws in the algo below.}
  558. procedure TranslatePathToMac (var path: string);
  559. var
  560. slashPos, oldpos, newpos, oldlen: Integer;
  561. inname: Boolean;
  562. begin
  563. slashPos := Pos('/', path);
  564. if (slashPos <> 0) then {its a unix path}
  565. begin
  566. if slashPos = 1 then {its a full path}
  567. begin
  568. Delete(path,1,1);
  569. Insert('{Boot}', path, 1);
  570. end
  571. else {its a partial path}
  572. Insert('/', path, 1);
  573. end
  574. else
  575. begin
  576. slashPos := Pos('\', path);
  577. if (slashPos <> 0) then {its a dos path}
  578. begin
  579. if slashPos = 1 then {its a full path, without drive letter}
  580. begin
  581. Delete(path,1,1);
  582. Insert('{Boot}', path, 1);
  583. end
  584. else if (Length(path) >= 2) and (path[2] = ':') then {its a full path, with drive letter}
  585. begin
  586. Delete(path, 1, 2);
  587. Insert('{Boot}', path, 1)
  588. end
  589. else {its a partial path}
  590. Insert('/', path, 1);
  591. end;
  592. end;
  593. if (slashPos <> 0) then {its a unix or dos path}
  594. begin
  595. {Translate "/../" to "::" , "/./" to ":" and "/" to ":" ) in place. }
  596. oldlen := Length(path);
  597. newpos := 0;
  598. oldpos := 0;
  599. inname := false;
  600. while oldpos < oldlen do
  601. begin
  602. oldpos := oldpos + 1;
  603. case path[oldpos] of
  604. '.':
  605. if (((oldpos < oldlen) and (path[oldpos + 1] in ['.', '/', '\'])) or (oldpos = oldlen)) and not inname then
  606. begin {its really a lonely ".." or "."}
  607. {Skip two chars in any case. }
  608. {For ".." then ".." is skiped and for "." then "./" is skiped, this}
  609. {reqires the next char is a "/". Thats why a "/" was }
  610. {appended on the end above.}
  611. oldpos := oldpos + 1;
  612. end
  613. else {its part of a filename (hidden unix file, e g ".nisse")}
  614. begin
  615. inname := true;
  616. newpos := newpos + 1;
  617. path[newpos] := path[oldpos];
  618. end;
  619. '/', '\':
  620. begin
  621. inname := false;
  622. newpos := newpos + 1;
  623. path[newpos] := ':'; {Exchange to mac dir separator.}
  624. end;
  625. 'A'..'Z' :
  626. begin
  627. inname := true;
  628. newpos := newpos + 1;
  629. path[newpos] :=char(byte(path[oldpos])+32);
  630. end;
  631. else
  632. begin
  633. inname := true;
  634. newpos := newpos + 1;
  635. path[newpos] := path[oldpos];
  636. end;
  637. end;
  638. end;
  639. SetLength(path,newpos);
  640. end;
  641. end;
  642. Function FixPath(s:string;allowdot:boolean):string;
  643. var
  644. i : longint;
  645. begin
  646. { Fix separator }
  647. for i:=1 to length(s) do
  648. if s[i] in ['/','\'] then
  649. s[i]:=source_info.DirSep;
  650. { Fix ending / }
  651. if (length(s)>0) and (s[length(s)]<>source_info.DirSep) and
  652. (s[length(s)]<>':') then
  653. s:=s+source_info.DirSep;
  654. { Remove ./ }
  655. if (not allowdot) and (s='.'+source_info.DirSep) then
  656. s:='';
  657. { return }
  658. if source_info.files_case_relevent then
  659. FixPath:=s
  660. else
  661. FixPath:=Lower(s);
  662. end;
  663. function FixFileName(const s:string):string;
  664. var
  665. i : longint;
  666. begin
  667. if source_info.system = system_powerpc_MACOS then
  668. begin
  669. FixFileName:= s;
  670. TranslatePathToMac(FixFileName);
  671. end
  672. else if source_info.files_case_relevent then
  673. begin
  674. for i:=1 to length(s) do
  675. begin
  676. case s[i] of
  677. '/','\' :
  678. FixFileName[i]:=source_info.dirsep;
  679. else
  680. FixFileName[i]:=s[i];
  681. end;
  682. end;
  683. FixFileName[0]:=s[0];
  684. end
  685. else
  686. begin
  687. for i:=1 to length(s) do
  688. begin
  689. case s[i] of
  690. '/','\' :
  691. FixFileName[i]:=source_info.dirsep;
  692. 'A'..'Z' :
  693. FixFileName[i]:=char(byte(s[i])+32);
  694. else
  695. FixFileName[i]:=s[i];
  696. end;
  697. end;
  698. FixFileName[0]:=s[0];
  699. end;
  700. end;
  701. Function TargetFixPath(s:string;allowdot:boolean):string;
  702. var
  703. i : longint;
  704. begin
  705. { Fix separator }
  706. for i:=1 to length(s) do
  707. if s[i] in ['/','\'] then
  708. s[i]:=target_info.DirSep;
  709. { Fix ending / }
  710. if (length(s)>0) and (s[length(s)]<>target_info.DirSep) and
  711. (s[length(s)]<>':') then
  712. s:=s+target_info.DirSep;
  713. { Remove ./ }
  714. if (not allowdot) and (s='.'+target_info.DirSep) then
  715. s:='';
  716. { return }
  717. if target_info.files_case_relevent then
  718. TargetFixPath:=s
  719. else
  720. TargetFixPath:=Lower(s);
  721. end;
  722. function TargetFixFileName(const s:string):string;
  723. var
  724. i : longint;
  725. begin
  726. if target_info.system = system_powerpc_MACOS then
  727. begin
  728. TargetFixFileName:= s;
  729. TranslatePathToMac(TargetFixFileName);
  730. end
  731. else if target_info.files_case_relevent then
  732. begin
  733. for i:=1 to length(s) do
  734. begin
  735. case s[i] of
  736. '/','\' :
  737. TargetFixFileName[i]:=target_info.dirsep;
  738. else
  739. TargetFixFileName[i]:=s[i];
  740. end;
  741. end;
  742. TargetFixFileName[0]:=s[0];
  743. end
  744. else
  745. begin
  746. for i:=1 to length(s) do
  747. begin
  748. case s[i] of
  749. '/','\' :
  750. TargetFixFileName[i]:=target_info.dirsep;
  751. 'A'..'Z' :
  752. TargetFixFileName[i]:=char(byte(s[i])+32);
  753. else
  754. TargetFixFileName[i]:=s[i];
  755. end;
  756. end;
  757. TargetFixFileName[0]:=s[0];
  758. end;
  759. end;
  760. procedure SplitBinCmd(const s:string;var bstr,cstr:string);
  761. var
  762. i : longint;
  763. begin
  764. i:=pos(' ',s);
  765. if i>0 then
  766. begin
  767. bstr:=Copy(s,1,i-1);
  768. cstr:=Copy(s,i+1,length(s)-i);
  769. end
  770. else
  771. begin
  772. bstr:=s;
  773. cstr:='';
  774. end;
  775. end;
  776. procedure TSearchPathList.AddPath(s:string;addfirst:boolean);
  777. begin
  778. AddPath('',s,AddFirst);
  779. end;
  780. procedure TSearchPathList.AddPath(SrcPath,s:string;addfirst:boolean);
  781. var
  782. j : longint;
  783. hs,hsd,
  784. CurrentDir,
  785. CurrPath : string;
  786. subdirfound : boolean;
  787. dir : searchrec;
  788. hp : TStringListItem;
  789. procedure addcurrpath;
  790. begin
  791. if addfirst then
  792. begin
  793. Remove(currPath);
  794. Insert(currPath);
  795. end
  796. else
  797. begin
  798. { Check if already in path, then we don't add it }
  799. hp:=Find(currPath);
  800. if not assigned(hp) then
  801. Concat(currPath);
  802. end;
  803. end;
  804. begin
  805. if s='' then
  806. exit;
  807. { Support default macro's }
  808. DefaultReplacements(s);
  809. { get current dir }
  810. CurrentDir:=GetCurrentDir;
  811. repeat
  812. { get currpath }
  813. if addfirst then
  814. begin
  815. j:=length(s);
  816. while (j>0) and (s[j]<>';') do
  817. dec(j);
  818. CurrPath:=FixPath(Copy(s,j+1,length(s)-j),false);
  819. if j=0 then
  820. s:=''
  821. else
  822. System.Delete(s,j,length(s)-j+1);
  823. end
  824. else
  825. begin
  826. j:=Pos(';',s);
  827. if j=0 then
  828. j:=255;
  829. CurrPath:=SrcPath+FixPath(Copy(s,1,j-1),false);
  830. System.Delete(s,1,j);
  831. end;
  832. { fix pathname }
  833. if CurrPath='' then
  834. CurrPath:='.'+source_info.DirSep
  835. else
  836. begin
  837. CurrPath:=FixPath(FExpand(CurrPath),false);
  838. if (CurrentDir<>'') and (Copy(CurrPath,1,length(CurrentDir))=CurrentDir) then
  839. begin
  840. {$ifdef AMIGA}
  841. CurrPath:=CurrentDir+Copy(CurrPath,length(CurrentDir)+1,255);
  842. {$else}
  843. CurrPath:='.'+source_info.DirSep+Copy(CurrPath,length(CurrentDir)+1,255);
  844. {$endif}
  845. end;
  846. end;
  847. { wildcard adding ? }
  848. if pos('*',currpath)>0 then
  849. begin
  850. if currpath[length(currpath)]=source_info.dirsep then
  851. hs:=Copy(currpath,1,length(CurrPath)-1)
  852. else
  853. hs:=currpath;
  854. hsd:=SplitPath(hs);
  855. findfirst(hs,directory,dir);
  856. subdirfound:=false;
  857. while doserror=0 do
  858. begin
  859. if (dir.name<>'.') and
  860. (dir.name<>'..') and
  861. ((dir.attr and directory)<>0) then
  862. begin
  863. subdirfound:=true;
  864. currpath:=hsd+dir.name+source_info.dirsep;
  865. hp:=Find(currPath);
  866. if not assigned(hp) then
  867. AddCurrPath;
  868. end;
  869. findnext(dir);
  870. if not subdirfound then
  871. WarnNonExistingPath(currpath);
  872. end;
  873. FindClose(dir);
  874. end
  875. else
  876. begin
  877. if PathExists(currpath) then
  878. addcurrpath
  879. else
  880. WarnNonExistingPath(currpath);
  881. end;
  882. until (s='');
  883. end;
  884. procedure TSearchPathList.AddList(list:TSearchPathList;addfirst:boolean);
  885. var
  886. s : string;
  887. hl : TSearchPathList;
  888. hp,hp2 : TStringListItem;
  889. begin
  890. if list.empty then
  891. exit;
  892. { create temp and reverse the list }
  893. if addfirst then
  894. begin
  895. hl:=TSearchPathList.Create;
  896. hp:=TStringListItem(list.first);
  897. while assigned(hp) do
  898. begin
  899. hl.insert(hp.Str);
  900. hp:=TStringListItem(hp.next);
  901. end;
  902. while not hl.empty do
  903. begin
  904. s:=hl.GetFirst;
  905. Remove(s);
  906. Insert(s);
  907. end;
  908. hl.Free;
  909. end
  910. else
  911. begin
  912. hp:=TStringListItem(list.first);
  913. while assigned(hp) do
  914. begin
  915. hp2:=Find(hp.Str);
  916. { Check if already in path, then we don't add it }
  917. if not assigned(hp2) then
  918. Concat(hp.Str);
  919. hp:=TStringListItem(hp.next);
  920. end;
  921. end;
  922. end;
  923. function TSearchPathList.FindFile(const f : string;var foundfile:string):boolean;
  924. Var
  925. p : TStringListItem;
  926. begin
  927. FindFile:=false;
  928. p:=TStringListItem(first);
  929. while assigned(p) do
  930. begin
  931. {
  932. Search order for case sensitive systems:
  933. 1. lowercase
  934. 2. NormalCase
  935. 3. UPPERCASE
  936. None case sensitive only lowercase
  937. }
  938. FoundFile:=p.Str+Lower(f);
  939. If FileExists(FoundFile) then
  940. begin
  941. FindFile:=true;
  942. exit;
  943. end;
  944. {$ifdef UNIX}
  945. FoundFile:=p.Str+f;
  946. If FileExists(FoundFile) then
  947. begin
  948. FindFile:=true;
  949. exit;
  950. end;
  951. FoundFile:=p.Str+Upper(f);
  952. If FileExists(FoundFile) then
  953. begin
  954. FindFile:=true;
  955. exit;
  956. end;
  957. {$endif UNIX}
  958. p:=TStringListItem(p.next);
  959. end;
  960. { Return original filename if not found }
  961. FoundFile:=f;
  962. end;
  963. Function GetFileTime ( Var F : File) : Longint;
  964. Var
  965. {$ifdef hasunix}
  966. info: Stat;
  967. {$endif}
  968. L : longint;
  969. begin
  970. {$ifdef hasunix}
  971. {$IFDEF havelinuxrtl10}
  972. FStat (F,Info);
  973. L:=Info.Mtime;
  974. {$ELSE}
  975. FPFStat (F,Info);
  976. L:=Info.st_Mtime;
  977. {$ENDIF}
  978. {$else}
  979. GetFTime(f,l);
  980. {$endif}
  981. GetFileTime:=L;
  982. end;
  983. Function GetNamedFileTime (Const F : String) : Longint;
  984. begin
  985. GetNamedFileTime:=do_getnamedfiletime(F);
  986. end;
  987. function FindFile(const f : string;path : string;var foundfile:string):boolean;
  988. Var
  989. singlepathstring : string;
  990. i : longint;
  991. begin
  992. {$ifdef Unix}
  993. for i:=1 to length(path) do
  994. if path[i]=':' then
  995. path[i]:=';';
  996. {$endif Unix}
  997. FindFile:=false;
  998. repeat
  999. i:=pos(';',path);
  1000. if i=0 then
  1001. i:=256;
  1002. singlepathstring:=FixPath(copy(path,1,i-1),false);
  1003. delete(path,1,i);
  1004. {
  1005. Search order for case sensitive systems:
  1006. 1. lowercase
  1007. 2. NormalCase
  1008. 3. UPPERCASE
  1009. None case sensitive only lowercase
  1010. }
  1011. FoundFile:=singlepathstring+Lower(f);
  1012. If FileExists(FoundFile) then
  1013. begin
  1014. FindFile:=true;
  1015. exit;
  1016. end;
  1017. {$ifdef UNIX}
  1018. FoundFile:=singlepathstring+f;
  1019. If FileExists(FoundFile) then
  1020. begin
  1021. FindFile:=true;
  1022. exit;
  1023. end;
  1024. FoundFile:=singlepathstring+Upper(f);
  1025. If FileExists(FoundFile) then
  1026. begin
  1027. FindFile:=true;
  1028. exit;
  1029. end;
  1030. {$endif UNIX}
  1031. until path='';
  1032. FoundFile:=f;
  1033. end;
  1034. function FindFilePchar(const f : string;path : pchar;var foundfile:string):boolean;
  1035. Var
  1036. singlepathstring : string;
  1037. startpc,pc : pchar;
  1038. sepch : char;
  1039. begin
  1040. {$ifdef Unix}
  1041. sepch:=':';
  1042. {$else}
  1043. sepch:=';';
  1044. {$endif Unix}
  1045. FindFilePchar:=false;
  1046. pc:=path;
  1047. repeat
  1048. startpc:=pc;
  1049. while (pc^<>sepch) and (pc^<>';') and (pc^<>#0) do
  1050. inc(pc);
  1051. move(startpc^,singlepathstring[1],pc-startpc);
  1052. singlepathstring[0]:=char(longint(pc-startpc));
  1053. singlepathstring:=FixPath(singlepathstring,false);
  1054. {
  1055. Search order for case sensitive systems:
  1056. 1. lowercase
  1057. 2. NormalCase
  1058. 3. UPPERCASE
  1059. None case sensitive only lowercase
  1060. }
  1061. FoundFile:=singlepathstring+Lower(f);
  1062. If FileExists(FoundFile) then
  1063. begin
  1064. FindFilePchar:=true;
  1065. exit;
  1066. end;
  1067. {$ifdef UNIX}
  1068. FoundFile:=singlepathstring+f;
  1069. If FileExists(FoundFile) then
  1070. begin
  1071. FindFilePchar:=true;
  1072. exit;
  1073. end;
  1074. FoundFile:=singlepathstring+Upper(f);
  1075. If FileExists(FoundFile) then
  1076. begin
  1077. FindFilePchar:=true;
  1078. exit;
  1079. end;
  1080. {$endif UNIX}
  1081. if (pc^=#0) then
  1082. break;
  1083. inc(pc);
  1084. until false;
  1085. end;
  1086. function FindExe(const bin:string;var foundfile:string):boolean;
  1087. var
  1088. p : pchar;
  1089. found : boolean;
  1090. begin
  1091. found:=FindFile(FixFileName(AddExtension(bin,source_info.exeext)),'.;'+exepath,foundfile);
  1092. if not found then
  1093. begin
  1094. p:=GetEnvPchar('PATH');
  1095. found:=FindFilePChar(FixFileName(AddExtension(bin,source_info.exeext)),p,foundfile);
  1096. FreeEnvPChar(p);
  1097. end;
  1098. FindExe:=found;
  1099. end;
  1100. function GetShortName(const n:string):string;
  1101. {$ifdef win32}
  1102. var
  1103. hs,hs2 : string;
  1104. i : longint;
  1105. {$endif}
  1106. {$ifdef go32v2}
  1107. var
  1108. hs : string;
  1109. {$endif}
  1110. {$ifdef watcom}
  1111. var
  1112. hs : string;
  1113. {$endif}
  1114. begin
  1115. GetShortName:=n;
  1116. {$ifdef win32}
  1117. hs:=n+#0;
  1118. i:=Windows.GetShortPathName(@hs[1],@hs2[1],high(hs2));
  1119. if (i>0) and (i<=high(hs2)) then
  1120. begin
  1121. hs2[0]:=chr(strlen(@hs2[1]));
  1122. GetShortName:=hs2;
  1123. end;
  1124. {$endif}
  1125. {$ifdef go32v2}
  1126. hs:=n;
  1127. if Dos.GetShortName(hs) then
  1128. GetShortName:=hs;
  1129. {$endif}
  1130. {$ifdef watcom}
  1131. hs:=n;
  1132. if Dos.GetShortName(hs) then
  1133. GetShortName:=hs;
  1134. {$endif}
  1135. end;
  1136. {****************************************************************************
  1137. OS Dependent things
  1138. ****************************************************************************}
  1139. function GetEnvPChar(const envname:string):pchar;
  1140. {$ifdef win32}
  1141. var
  1142. s : string;
  1143. i,len : longint;
  1144. hp,p,p2 : pchar;
  1145. {$endif}
  1146. begin
  1147. {$ifdef hasunix}
  1148. GetEnvPchar:={$ifdef havelinuxrtl10}Linux.getenv{$else}BaseUnix.fpGetEnv{$endif}(envname);
  1149. {$define GETENVOK}
  1150. {$endif}
  1151. {$ifdef win32}
  1152. GetEnvPchar:=nil;
  1153. p:=GetEnvironmentStrings;
  1154. hp:=p;
  1155. while hp^<>#0 do
  1156. begin
  1157. s:=strpas(hp);
  1158. i:=pos('=',s);
  1159. len:=strlen(hp);
  1160. if upper(copy(s,1,i-1))=upper(envname) then
  1161. begin
  1162. GetMem(p2,len-length(envname));
  1163. Move(hp[i],p2^,len-length(envname));
  1164. GetEnvPchar:=p2;
  1165. break;
  1166. end;
  1167. { next string entry}
  1168. hp:=hp+len+1;
  1169. end;
  1170. FreeEnvironmentStrings(p);
  1171. {$define GETENVOK}
  1172. {$endif}
  1173. {$ifdef os2}
  1174. GetEnvPChar := Dos.GetEnvPChar (EnvName);
  1175. {$define GETENVOK}
  1176. {$endif}
  1177. {$ifdef GETENVOK}
  1178. {$undef GETENVOK}
  1179. {$else}
  1180. GetEnvPchar:=StrPNew({$ifdef delphi}DMisc{$else}Dos{$endif}.Getenv(envname));
  1181. {$endif}
  1182. end;
  1183. procedure FreeEnvPChar(p:pchar);
  1184. begin
  1185. {$ifndef hasunix}
  1186. {$ifndef os2}
  1187. StrDispose(p);
  1188. {$endif}
  1189. {$endif}
  1190. end;
  1191. function Shell(const command:string): longint;
  1192. { This is already defined in the linux.ppu for linux, need for the *
  1193. expansion under linux }
  1194. {$ifdef hasunix}
  1195. begin
  1196. result := {$ifdef havelinuxrtl10}Linux{$else}Unix{$endif}.Shell(command);
  1197. end;
  1198. {$else}
  1199. {$ifdef amiga}
  1200. begin
  1201. exec('',command);
  1202. if (doserror <> 0) then
  1203. result := doserror
  1204. else
  1205. result := dosexitcode;
  1206. end;
  1207. {$else}
  1208. var
  1209. comspec : string;
  1210. begin
  1211. comspec:=getenv('COMSPEC');
  1212. Exec(comspec,' /C '+command);
  1213. if (doserror <> 0) then
  1214. result := doserror
  1215. else
  1216. result := dosexitcode;
  1217. end;
  1218. {$endif}
  1219. {$endif}
  1220. {$ifdef CPUI386}
  1221. { later, this should be replaced by the math unit }
  1222. const
  1223. Default8087CW : word = $1332;
  1224. {$ASMMODE ATT}
  1225. procedure Set8087CW(cw:word);assembler;
  1226. asm
  1227. movw cw,%ax
  1228. movw %ax,default8087cw
  1229. fnclex
  1230. fldcw default8087cw
  1231. end;
  1232. function Get8087CW:word;assembler;
  1233. asm
  1234. pushl $0
  1235. fnstcw (%esp)
  1236. popl %eax
  1237. end;
  1238. function SetFPUExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  1239. var
  1240. CtlWord: Word;
  1241. begin
  1242. CtlWord:=Get8087CW;
  1243. Set8087CW( (CtlWord and $FFC0) or Byte(Longint(Mask)) );
  1244. Result:=TFPUExceptionMask(Longint(CtlWord and $3F));
  1245. end;
  1246. {$else CPUI386}
  1247. {$ifdef CPUPOWERPC}
  1248. function SetFPUExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  1249. var
  1250. newmask: record
  1251. case byte of
  1252. 1: (d: double);
  1253. 2: (a,b: cardinal);
  1254. end;
  1255. begin
  1256. { load current control register contents }
  1257. asm
  1258. mffs f0
  1259. stfd f0,newmask.d
  1260. end;
  1261. { invalid operation: bit 24 (big endian, bit 0 = left-most bit) }
  1262. if (exInvalidOp in mask) then
  1263. newmask.b := newmask.b and not(1 shl (31-24))
  1264. else
  1265. newmask.b := newmask.b or (1 shl (31-24));
  1266. { denormals can not cause exceptions on the PPC }
  1267. { zero divide: bit 27 }
  1268. if (exZeroDivide in mask) then
  1269. newmask.b := newmask.b and not(1 shl (31-27))
  1270. else
  1271. newmask.b := newmask.b or (1 shl (31-27));
  1272. { overflow: bit 25 }
  1273. if (exOverflow in mask) then
  1274. newmask.b := newmask.b and not(1 shl (31-25))
  1275. else
  1276. newmask.b := newmask.b or (1 shl (31-25));
  1277. { underflow: bit 26 }
  1278. if (exUnderflow in mask) then
  1279. newmask.b := newmask.b and not(1 shl (31-26))
  1280. else
  1281. newmask.b := newmask.b or (1 shl (31-26));
  1282. { Precision (inexact result): bit 28 }
  1283. if (exUnderflow in mask) then
  1284. newmask.b := newmask.b and not(1 shl (31-28))
  1285. else
  1286. newmask.b := newmask.b or (1 shl (31-28));
  1287. { update control register contents }
  1288. asm
  1289. lfd f0, newmask.d
  1290. mtfsf 255,f0
  1291. end;
  1292. end;
  1293. {$else CPUPOWERPC}
  1294. function SetFPUExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  1295. begin
  1296. end;
  1297. {$endif CPUPOWERPC}
  1298. {$endif CPUI386}
  1299. function is_number_float(d : double) : boolean;
  1300. var
  1301. bytearray : array[0..7] of byte;
  1302. begin
  1303. move(d,bytearray,8);
  1304. { only 1.1 save, 1.0.x will use always little endian }
  1305. {$ifdef FPC_BIG_ENDIAN}
  1306. result:=((bytearray[0] and $7f)<>$7f) or ((bytearray[1] and $f0)<>$f0);
  1307. {$else FPC_BIG_ENDIAN}
  1308. result:=((bytearray[7] and $7f)<>$7f) or ((bytearray[6] and $f0)<>$f0);
  1309. {$endif FPC_BIG_ENDIAN}
  1310. end;
  1311. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  1312. var
  1313. b : boolean;
  1314. begin
  1315. b:=true;
  1316. if s='DEFAULT' then
  1317. aktmodeswitches:=initmodeswitches
  1318. else
  1319. if s='DELPHI' then
  1320. aktmodeswitches:=delphimodeswitches
  1321. else
  1322. if s='TP' then
  1323. aktmodeswitches:=tpmodeswitches
  1324. else
  1325. if s='FPC' then
  1326. aktmodeswitches:=fpcmodeswitches
  1327. else
  1328. if s='OBJFPC' then
  1329. aktmodeswitches:=objfpcmodeswitches
  1330. else
  1331. if s='GPC' then
  1332. aktmodeswitches:=gpcmodeswitches
  1333. else
  1334. if s='MACPAS' then
  1335. aktmodeswitches:=macmodeswitches
  1336. else
  1337. b:=false;
  1338. if b and changeInit then
  1339. initmodeswitches := aktmodeswitches;
  1340. if b then
  1341. begin
  1342. { turn ansistrings on by default ? }
  1343. if (m_delphi in aktmodeswitches) then
  1344. begin
  1345. include(aktlocalswitches,cs_ansistrings);
  1346. if changeinit then
  1347. include(initlocalswitches,cs_ansistrings);
  1348. end
  1349. else
  1350. begin
  1351. exclude(aktlocalswitches,cs_ansistrings);
  1352. if changeinit then
  1353. exclude(initlocalswitches,cs_ansistrings);
  1354. end;
  1355. { Default enum packing for delphi/tp7 }
  1356. if (m_tp7 in aktmodeswitches) or
  1357. (m_delphi in aktmodeswitches) then
  1358. aktpackenum:=1
  1359. else
  1360. aktpackenum:=4;
  1361. if changeinit then
  1362. initpackenum:=aktpackenum;
  1363. {$ifdef i386}
  1364. { Default to intel assembler for delphi/tp7 on i386 }
  1365. if (m_delphi in aktmodeswitches) or
  1366. (m_tp7 in aktmodeswitches) then
  1367. aktasmmode:=asmmode_i386_intel;
  1368. if changeinit then
  1369. initasmmode:=aktasmmode;
  1370. {$endif i386}
  1371. end;
  1372. SetCompileMode:=b;
  1373. end;
  1374. function SetAktProcCall(const s:string; changeInit:boolean):boolean;
  1375. const
  1376. DefProcCallName : array[tproccalloption] of string[12] = ('',
  1377. 'CDECL',
  1378. 'CPPDECL',
  1379. '', { compilerproc }
  1380. 'FAR16',
  1381. 'FPCCALL',
  1382. 'INLINE',
  1383. '', { internproc }
  1384. '', { syscall }
  1385. 'PASCAL',
  1386. 'REGISTER',
  1387. 'SAFECALL',
  1388. 'STDCALL',
  1389. 'SOFTFLOAT'
  1390. );
  1391. var
  1392. t : tproccalloption;
  1393. begin
  1394. result:=false;
  1395. for t:=low(tproccalloption) to high(tproccalloption) do
  1396. if DefProcCallName[t]=s then
  1397. begin
  1398. AktDefProcCall:=t;
  1399. result:=true;
  1400. break;
  1401. end;
  1402. if changeinit then
  1403. InitDefProcCall:=AktDefProcCall;
  1404. end;
  1405. function SetProcessor(const s:string; changeInit: boolean):boolean;
  1406. var
  1407. t : tprocessors;
  1408. begin
  1409. SetProcessor:=false;
  1410. for t:=low(tprocessors) to high(tprocessors) do
  1411. if processorsstr[t]=s then
  1412. begin
  1413. aktspecificoptprocessor:=t;
  1414. SetProcessor:=true;
  1415. break;
  1416. end;
  1417. if changeinit then
  1418. initspecificoptprocessor:=aktspecificoptprocessor;
  1419. end;
  1420. function SetFpuType(const s:string; changeInit: boolean):boolean;
  1421. var
  1422. t : tfputype;
  1423. begin
  1424. SetFpuType:=false;
  1425. for t:=low(tfputype) to high(tfputype) do
  1426. if fputypestr[t]=s then
  1427. begin
  1428. aktfputype:=t;
  1429. SetFpuType:=true;
  1430. break;
  1431. end;
  1432. if changeinit then
  1433. initfputype:=aktfputype;
  1434. end;
  1435. { '('D1:'00000000-'D2:'0000-'D3:'0000-'D4:'0000-000000000000)' }
  1436. function string2guid(const s: string; var GUID: TGUID): boolean;
  1437. function ishexstr(const hs: string): boolean;
  1438. var
  1439. i: integer;
  1440. begin
  1441. ishexstr:=false;
  1442. for i:=1 to Length(hs) do begin
  1443. if not (hs[i] in ['0'..'9','A'..'F','a'..'f']) then
  1444. exit;
  1445. end;
  1446. ishexstr:=true;
  1447. end;
  1448. function hexstr2longint(const hexs: string): longint;
  1449. var
  1450. i: integer;
  1451. rl: longint;
  1452. begin
  1453. rl:=0;
  1454. for i:=1 to length(hexs) do begin
  1455. rl:=rl shl 4;
  1456. case hexs[i] of
  1457. '0'..'9' : inc(rl,ord(hexs[i])-ord('0'));
  1458. 'A'..'F' : inc(rl,ord(hexs[i])-ord('A')+10);
  1459. 'a'..'f' : inc(rl,ord(hexs[i])-ord('a')+10);
  1460. end
  1461. end;
  1462. hexstr2longint:=rl;
  1463. end;
  1464. var
  1465. i: integer;
  1466. begin
  1467. if (Length(s)=38) and (s[1]='{') and (s[38]='}') and
  1468. (s[10]='-') and (s[15]='-') and (s[20]='-') and (s[25]='-') and
  1469. ishexstr(copy(s,2,8)) and ishexstr(copy(s,11,4)) and
  1470. ishexstr(copy(s,16,4)) and ishexstr(copy(s,21,4)) and
  1471. ishexstr(copy(s,26,12)) then begin
  1472. GUID.D1:=dword(hexstr2longint(copy(s,2,8)));
  1473. { these values are arealdy in the correct range (4 chars = word) }
  1474. GUID.D2:=word(hexstr2longint(copy(s,11,4)));
  1475. GUID.D3:=word(hexstr2longint(copy(s,16,4)));
  1476. for i:=0 to 1 do
  1477. GUID.D4[i]:=byte(hexstr2longint(copy(s,21+i*2,2)));
  1478. for i:=2 to 7 do
  1479. GUID.D4[i]:=byte(hexstr2longint(copy(s,22+i*2,2)));
  1480. string2guid:=true;
  1481. end
  1482. else
  1483. string2guid:=false;
  1484. end;
  1485. function guid2string(const GUID: TGUID): string;
  1486. function long2hex(l, len: longint): string;
  1487. const
  1488. hextbl: array[0..15] of char = '0123456789ABCDEF';
  1489. var
  1490. rs: string;
  1491. i: integer;
  1492. begin
  1493. rs[0]:=chr(len);
  1494. for i:=len downto 1 do begin
  1495. rs[i]:=hextbl[l and $F];
  1496. l:=l shr 4;
  1497. end;
  1498. long2hex:=rs;
  1499. end;
  1500. begin
  1501. guid2string:=
  1502. '{'+long2hex(GUID.D1,8)+
  1503. '-'+long2hex(GUID.D2,4)+
  1504. '-'+long2hex(GUID.D3,4)+
  1505. '-'+long2hex(GUID.D4[0],2)+long2hex(GUID.D4[1],2)+
  1506. '-'+long2hex(GUID.D4[2],2)+long2hex(GUID.D4[3],2)+
  1507. long2hex(GUID.D4[4],2)+long2hex(GUID.D4[5],2)+
  1508. long2hex(GUID.D4[6],2)+long2hex(GUID.D4[7],2)+
  1509. '}';
  1510. end;
  1511. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  1512. var
  1513. tok : string;
  1514. vstr : string;
  1515. l : longint;
  1516. code : integer;
  1517. b : talignmentinfo;
  1518. begin
  1519. UpdateAlignmentStr:=true;
  1520. uppervar(s);
  1521. fillchar(b,sizeof(b),0);
  1522. repeat
  1523. tok:=GetToken(s,'=');
  1524. if tok='' then
  1525. break;
  1526. vstr:=GetToken(s,',');
  1527. val(vstr,l,code);
  1528. if tok='PROC' then
  1529. b.procalign:=l
  1530. else if tok='JUMP' then
  1531. b.jumpalign:=l
  1532. else if tok='LOOP' then
  1533. b.loopalign:=l
  1534. else if tok='CONSTMIN' then
  1535. b.constalignmin:=l
  1536. else if tok='CONSTMAX' then
  1537. b.constalignmax:=l
  1538. else if tok='VARMIN' then
  1539. b.varalignmin:=l
  1540. else if tok='VARMAX' then
  1541. b.varalignmax:=l
  1542. else if tok='LOCALMIN' then
  1543. b.localalignmin:=l
  1544. else if tok='LOCALMAX' then
  1545. b.localalignmax:=l
  1546. else if tok='RECORDMIN' then
  1547. b.recordalignmin:=l
  1548. else if tok='RECORDMAX' then
  1549. b.recordalignmax:=l
  1550. else { Error }
  1551. UpdateAlignmentStr:=false;
  1552. until false;
  1553. UpdateAlignment(a,b);
  1554. end;
  1555. function var_align(siz: longint): longint;
  1556. begin
  1557. siz := size_2_align(siz);
  1558. var_align := used_align(siz,aktalignment.varalignmin,aktalignment.varalignmax);
  1559. end;
  1560. function const_align(siz: longint): longint;
  1561. begin
  1562. siz := size_2_align(siz);
  1563. const_align := used_align(siz,aktalignment.constalignmin,aktalignment.constalignmax);
  1564. end;
  1565. {****************************************************************************
  1566. Init
  1567. ****************************************************************************}
  1568. {$ifdef unix}
  1569. {$define need_path_search}
  1570. {$endif unix}
  1571. {$ifdef os2}
  1572. {$define need_path_search}
  1573. {$endif os2}
  1574. procedure get_exepath;
  1575. var
  1576. hs1 : namestr;
  1577. hs2 : extstr;
  1578. p : pchar;
  1579. begin
  1580. {$ifdef delphi}
  1581. exepath:=dmisc.getenv('PPC_EXEC_PATH');
  1582. {$else delphi}
  1583. exepath:=dos.getenv('PPC_EXEC_PATH');
  1584. {$endif delphi}
  1585. if exepath='' then
  1586. fsplit(FixFileName(system.paramstr(0)),exepath,hs1,hs2);
  1587. {$ifdef need_path_search}
  1588. if exepath='' then
  1589. begin
  1590. if pos(source_info.exeext,hs1) <>
  1591. (length(hs1) - length(source_info.exeext)+1) then
  1592. hs1 := hs1 + source_info.exeext;
  1593. p:=GetEnvPchar('PATH');
  1594. FindFilePChar(hs1,p,exepath);
  1595. FreeEnvPChar(p);
  1596. exepath:=SplitPath(exepath);
  1597. end;
  1598. {$endif need_path_search}
  1599. exepath:=FixPath(exepath,false);
  1600. end;
  1601. procedure DoneGlobals;
  1602. begin
  1603. initdefines.free;
  1604. if assigned(DLLImageBase) then
  1605. StringDispose(DLLImageBase);
  1606. librarysearchpath.Free;
  1607. unitsearchpath.Free;
  1608. objectsearchpath.Free;
  1609. includesearchpath.Free;
  1610. end;
  1611. procedure InitGlobals;
  1612. begin
  1613. get_exepath;
  1614. { reset globals }
  1615. do_build:=false;
  1616. do_release:=false;
  1617. do_make:=true;
  1618. compile_level:=0;
  1619. DLLsource:=false;
  1620. inlining_procedure:=false;
  1621. resolving_forward:=false;
  1622. make_ref:=false;
  1623. LinkTypeSetExplicitly:=false;
  1624. { Output }
  1625. OutputFile:='';
  1626. OutputPrefix:=Nil;
  1627. OutputSuffix:=Nil;
  1628. OutputExtension:='';
  1629. OutputExeDir:='';
  1630. OutputUnitDir:='';
  1631. { Utils directory }
  1632. utilsdirectory:='';
  1633. utilsprefix:='';
  1634. cshared:=false;
  1635. rlinkpath:='';
  1636. { Search Paths }
  1637. librarysearchpath:=TSearchPathList.Create;
  1638. unitsearchpath:=TSearchPathList.Create;
  1639. includesearchpath:=TSearchPathList.Create;
  1640. objectsearchpath:=TSearchPathList.Create;
  1641. { Def file }
  1642. usewindowapi:=false;
  1643. description:='Compiled by FPC '+version_string+' - '+target_cpu_string;
  1644. DescriptionSetExplicity:=false;
  1645. dllversion:='';
  1646. dllmajor:=1;
  1647. dllminor:=0;
  1648. dllrevision:=0;
  1649. nwscreenname := '';
  1650. nwthreadname := '';
  1651. nwcopyright := '';
  1652. UseDeffileForExports:=false;
  1653. UseDeffileForExportsSetExplicitly:=false;
  1654. RelocSection:=false;
  1655. RelocSectionSetExplicitly:=false;
  1656. LinkTypeSetExplicitly:=false;
  1657. { Init values }
  1658. initmodeswitches:=fpcmodeswitches;
  1659. initlocalswitches:=[cs_check_io,cs_typed_const_writable];
  1660. initmoduleswitches:=[cs_extsyntax,cs_implicit_exceptions];
  1661. initsourcecodepage:='8859-1';
  1662. initglobalswitches:=[cs_check_unit_name,cs_link_static{$ifdef INTERNALLINKER},cs_link_internal,cs_link_map{$endif}];
  1663. initoutputformat:=target_asm.id;
  1664. fillchar(initalignment,sizeof(talignmentinfo),0);
  1665. { might be overridden later }
  1666. initasmmode:=asmmode_standard;
  1667. {$ifdef i386}
  1668. initoptprocessor:=Class386;
  1669. initspecificoptprocessor:=Class386;
  1670. initfputype:=fpu_x87;
  1671. initpackenum:=4;
  1672. {$IFDEF testvarsets}
  1673. initsetalloc:=0;
  1674. {$ENDIF}
  1675. initasmmode:=asmmode_i386_att;
  1676. {$endif i386}
  1677. {$ifdef m68k}
  1678. initoptprocessor:=MC68020;
  1679. initpackenum:=4;
  1680. {$IFDEF testvarsets}
  1681. initsetalloc:=0;
  1682. {$ENDIF}
  1683. {$endif m68k}
  1684. {$ifdef powerpc}
  1685. initoptprocessor:=PPC604;
  1686. initpackenum:=4;
  1687. {$IFDEF testvarsets}
  1688. initsetalloc:=0;
  1689. {$ENDIF}
  1690. initfputype:=fpu_standard;
  1691. {$endif powerpc}
  1692. {$ifdef sparc}
  1693. initoptprocessor:=SPARC_V8;
  1694. initpackenum:=4;
  1695. {$IFDEF testvarsets}
  1696. initsetalloc:=0;
  1697. {$ENDIF}
  1698. {$endif sparc}
  1699. {$ifdef arm}
  1700. initpackenum:=4;
  1701. {$IFDEF testvarsets}
  1702. initsetalloc:=0;
  1703. {$ENDIF}
  1704. initfputype:=fpu_fpa;
  1705. {$endif arm}
  1706. {$ifdef x86_64}
  1707. initoptprocessor:=ClassAthlon64;
  1708. initspecificoptprocessor:=ClassAthlon64;
  1709. initfputype:=fpu_sse64;
  1710. initpackenum:=4;
  1711. {$IFDEF testvarsets}
  1712. initsetalloc:=0;
  1713. {$ENDIF}
  1714. initasmmode:=asmmode_x86_64_gas;
  1715. {$endif x86_64}
  1716. initinterfacetype:=it_interfacecom;
  1717. initdefproccall:=pocall_default;
  1718. initdefines:=TStringList.Create;
  1719. { memory sizes, will be overriden by parameter or default for target
  1720. in options or init_parser }
  1721. stacksize:=0;
  1722. heapsize:=0;
  1723. apptype:=app_cui;
  1724. end;
  1725. end.
  1726. {
  1727. $Log$
  1728. Revision 1.134 2004-08-02 07:15:54 michael
  1729. + Patch from Christian Iversen to implement LIBPREFIX/SUFFIX/EXTENSION directives
  1730. Revision 1.133 2004/07/17 15:51:57 jonas
  1731. * shell now returns an exitcode
  1732. * print an error if linking failed when linking was done using a script
  1733. Revision 1.132 2004/07/05 23:28:24 olle
  1734. + FixFileName now handles Mac OS paths
  1735. Revision 1.131 2004/06/20 08:55:29 florian
  1736. * logs truncated
  1737. Revision 1.130 2004/06/16 20:07:07 florian
  1738. * dwarf branch merged
  1739. Revision 1.129 2004/05/11 18:20:52 olle
  1740. * changed $mode mac to $mode macpas
  1741. * changed macmodeswitches to be more faithful to the mac dialect
  1742. Revision 1.128 2004/04/29 19:56:36 daniel
  1743. * Prepare compiler infrastructure for multiple ansistring types
  1744. Revision 1.127 2004/04/28 15:19:03 florian
  1745. + syscall directive support for MorphOS added
  1746. Revision 1.126.2.4 2004/05/09 15:47:21 peter
  1747. * fix typecast from word->set
  1748. }