globals.pas 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967
  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 ver1_0}
  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_class,m_result,m_repeat_forward];
  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. { maximum of units which are supported for a compilation }
  70. maxunits = 1024;
  71. treelogfilename = 'tree.log';
  72. { I don't know if this endian dependend }
  73. MathQNaN : array[0..7] of byte = (0,0,0,0,0,0,252,255);
  74. MathInf : array[0..7] of byte = (0,0,0,0,0,0,240,127);
  75. MathNegInf : array[0..7] of byte = (0,0,0,0,0,0,240,255);
  76. type
  77. TFPUException = (exInvalidOp, exDenormalized, exZeroDivide,
  78. exOverflow, exUnderflow, exPrecision);
  79. TFPUExceptionMask = set of TFPUException;
  80. pfileposinfo = ^tfileposinfo;
  81. tfileposinfo = record
  82. line : longint;
  83. column : word;
  84. fileindex : word;
  85. { moduleindex : word; }
  86. end;
  87. TSearchPathList = class(TStringList)
  88. procedure AddPath(s:string;addfirst:boolean);overload;
  89. procedure AddPath(SrcPath,s:string;addfirst:boolean);overload;
  90. procedure AddList(list:TSearchPathList;addfirst:boolean);
  91. function FindFile(const f : string;var foundfile:string):boolean;
  92. end;
  93. tcodepagestring = string[20];
  94. { the ordinal type used when evaluating constant integer expressions }
  95. TConstExprInt = int64;
  96. { ... the same unsigned }
  97. TConstExprUInt = {$ifdef fpc}qword{$else}int64{$endif};
  98. var
  99. { specified inputfile }
  100. inputdir : dirstr;
  101. inputfile : namestr;
  102. inputextension : extstr;
  103. { specified outputfile with -o parameter }
  104. outputfile : namestr;
  105. { specified with -FE or -FU }
  106. outputexedir : dirstr;
  107. outputunitdir : dirstr;
  108. { things specified with parameters }
  109. paralinkoptions,
  110. paradynamiclinker : string;
  111. paraprintnodetree : byte;
  112. parapreprocess : boolean;
  113. printnodefile : text;
  114. { typical cross compiling params}
  115. { directory where the utils can be found (options -FD) }
  116. utilsdirectory : dirstr;
  117. { targetname specific prefix used by these utils (options -XP<path>) }
  118. utilsprefix : dirstr;
  119. { Don't add std paths to linkpath}
  120. Dontlinkstdlibpath: Boolean;
  121. { some flags for global compiler switches }
  122. do_build,
  123. do_release,
  124. do_make : boolean;
  125. { path for searching units, different paths can be seperated by ; }
  126. exepath : dirstr; { Path to ppc }
  127. librarysearchpath,
  128. unitsearchpath,
  129. objectsearchpath,
  130. includesearchpath : TSearchPathList;
  131. { deffile }
  132. usewindowapi : boolean;
  133. description : string;
  134. dllversion : string;
  135. dllmajor,dllminor,dllrevision : word; { revision only for netware }
  136. akttokenpos, { position of the last token }
  137. aktfilepos : tfileposinfo; { current position }
  138. nwscreenname : string;
  139. nwthreadname : string;
  140. nwcopyright : string;
  141. codegenerror : boolean; { true if there is an error reported }
  142. block_type : tblock_type; { type of currently parsed block }
  143. parsing_para_level : integer; { parameter level, used to convert
  144. proc calls to proc loads in firstcalln }
  145. compile_level : word;
  146. make_ref : boolean;
  147. resolving_forward : boolean; { used to add forward reference as second ref }
  148. inlining_procedure : boolean; { are we inlining a procedure }
  149. exceptblockcounter : integer; { each except block gets a unique number check gotos }
  150. aktexceptblock : integer; { the exceptblock number of the current block (0 if none) }
  151. { commandline values }
  152. initdefines : tstringlist;
  153. initglobalswitches : tglobalswitches;
  154. initmoduleswitches : tmoduleswitches;
  155. initlocalswitches : tlocalswitches;
  156. initmodeswitches : tmodeswitches;
  157. {$IFDEF testvarsets}
  158. Initsetalloc, {0=fixed, 1 =var}
  159. {$ENDIF}
  160. initpackenum : longint;
  161. initalignment : talignmentinfo;
  162. initoptprocessor,
  163. initspecificoptprocessor : tprocessors;
  164. initfputype : tfputype;
  165. initasmmode : tasmmode;
  166. initinterfacetype : tinterfacetypes;
  167. initoutputformat : tasm;
  168. initdefproccall : tproccalloption;
  169. initsourcecodepage : tcodepagestring;
  170. { current state values }
  171. aktglobalswitches : tglobalswitches;
  172. aktmoduleswitches : tmoduleswitches;
  173. aktlocalswitches : tlocalswitches;
  174. nextaktlocalswitches : tlocalswitches;
  175. localswitcheschanged : boolean;
  176. aktmodeswitches : tmodeswitches;
  177. {$IFDEF testvarsets}
  178. aktsetalloc,
  179. {$ENDIF}
  180. aktpackenum : longint;
  181. aktmaxfpuregisters : longint;
  182. aktalignment : talignmentinfo;
  183. aktoptprocessor,
  184. aktspecificoptprocessor : tprocessors;
  185. aktfputype : tfputype;
  186. aktasmmode : tasmmode;
  187. aktinterfacetype : tinterfacetypes;
  188. aktoutputformat : tasm;
  189. aktdefproccall : tproccalloption;
  190. aktsourcecodepage : tcodepagestring;
  191. { Memory sizes }
  192. heapsize,
  193. stacksize : longint;
  194. {$Ifdef EXTDEBUG}
  195. { parameter switches }
  196. debugstop : boolean;
  197. {$EndIf EXTDEBUG}
  198. { windows / OS/2 application type }
  199. apptype : tapptype;
  200. const
  201. RelocSection : boolean = true;
  202. RelocSectionSetExplicitly : boolean = false;
  203. LinkTypeSetExplicitly : boolean = false;
  204. DLLsource : boolean = false;
  205. DLLImageBase : pstring = nil;
  206. UseDeffileForExport : boolean = true;
  207. ForceDeffileForExport : boolean = false;
  208. { used to set all registers used for each global function
  209. this should dramatically decrease the number of
  210. recompilations needed PM }
  211. simplify_ppu : boolean = true;
  212. { should we allow non static members ? }
  213. allow_only_static : boolean = false;
  214. Inside_asm_statement : boolean = false;
  215. global_unit_count : word = 0;
  216. { for error info in pp.pas }
  217. parser_current_file : string = '';
  218. {$ifdef m68k}
  219. { PalmOS resources }
  220. palmos_applicationname : string = 'FPC Application';
  221. palmos_applicationid : string[4] = 'FPCA';
  222. {$endif m68k}
  223. procedure abstract;
  224. function bstoslash(const s : string) : string;
  225. function getdatestr:string;
  226. function gettimestr:string;
  227. function filetimestring( t : longint) : string;
  228. procedure DefaultReplacements(var s:string);
  229. function GetCurrentDir:string;
  230. function path_absolute(const s : string) : boolean;
  231. Function PathExists ( F : String) : Boolean;
  232. Function FileExists ( Const F : String) : Boolean;
  233. Function RemoveFile(const f:string):boolean;
  234. Function RemoveDir(d:string):boolean;
  235. Function GetFileTime ( Var F : File) : Longint;
  236. Function GetNamedFileTime ( Const F : String) : Longint;
  237. Function SplitPath(const s:string):string;
  238. Function SplitFileName(const s:string):string;
  239. Function SplitName(const s:string):string;
  240. Function SplitExtension(Const HStr:String):String;
  241. Function AddExtension(Const HStr,ext:String):String;
  242. Function ForceExtension(Const HStr,ext:String):String;
  243. Function FixPath(s:string;allowdot:boolean):string;
  244. function FixFileName(const s:string):string;
  245. function TargetFixPath(s:string;allowdot:boolean):string;
  246. function TargetFixFileName(const s:string):string;
  247. procedure SplitBinCmd(const s:string;var bstr,cstr:string);
  248. function FindFile(const f : string;path : string;var foundfile:string):boolean;
  249. function FindFilePchar(const f : string;path : pchar;var foundfile:string):boolean;
  250. function FindExe(const bin:string;var foundfile:string):boolean;
  251. function GetShortName(const n:string):string;
  252. Procedure Shell(const command:string);
  253. function GetEnvPChar(const envname:string):pchar;
  254. procedure FreeEnvPChar(p:pchar);
  255. function SetFPUExceptionMask(const Mask : TFPUExceptionMask) : TFPUExceptionMask;
  256. function is_number_float(d : double) : boolean;
  257. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  258. function SetAktProcCall(const s:string; changeInit: boolean):boolean;
  259. procedure InitGlobals;
  260. procedure DoneGlobals;
  261. function string2guid(const s: string; var GUID: TGUID): boolean;
  262. function guid2string(const GUID: TGUID): string;
  263. procedure swap_qword(var q : qword);
  264. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  265. {# Routine to get the required alignment for size of data, which will
  266. be placed in bss segment, according to the current alignment requirements }
  267. function var_align(siz: longint): longint;
  268. {# Routine to get the required alignment for size of data, which will
  269. be placed in data/const segment, according to the current alignment requirements }
  270. function const_align(siz: longint): longint;
  271. implementation
  272. uses
  273. comphook;
  274. procedure abstract;
  275. begin
  276. do_internalerror(255);
  277. end;
  278. procedure WarnNonExistingPath(const path : string);
  279. begin
  280. if assigned({$ifndef FPCPROCVAR}@{$endif}do_comment) then
  281. do_comment(V_Hint,'Path "'+path+'" not found');
  282. end;
  283. function bstoslash(const s : string) : string;
  284. {
  285. return string s with all \ changed into /
  286. }
  287. var
  288. i : longint;
  289. begin
  290. for i:=1to length(s) do
  291. if s[i]='\' then
  292. bstoslash[i]:='/'
  293. else
  294. bstoslash[i]:=s[i];
  295. bstoslash[0]:=s[0];
  296. end;
  297. {****************************************************************************
  298. Time Handling
  299. ****************************************************************************}
  300. Function L0(l:longint):string;
  301. {
  302. return the string of value l, if l<10 then insert a zero, so
  303. the string is always at least 2 chars '01','02',etc
  304. }
  305. var
  306. s : string;
  307. begin
  308. Str(l,s);
  309. if l<10 then
  310. s:='0'+s;
  311. L0:=s;
  312. end;
  313. function gettimestr:string;
  314. {
  315. get the current time in a string HH:MM:SS
  316. }
  317. var
  318. hour,min,sec,hsec : word;
  319. begin
  320. {$ifdef delphi}
  321. dmisc.gettime(hour,min,sec,hsec);
  322. {$else delphi}
  323. dos.gettime(hour,min,sec,hsec);
  324. {$endif delphi}
  325. gettimestr:=L0(Hour)+':'+L0(min)+':'+L0(sec);
  326. end;
  327. function getdatestr:string;
  328. {
  329. get the current date in a string YY/MM/DD
  330. }
  331. var
  332. Year,Month,Day,Wday : Word;
  333. begin
  334. {$ifdef delphi}
  335. dmisc.getdate(year,month,day,wday);
  336. {$else}
  337. dos.getdate(year,month,day,wday);
  338. {$endif}
  339. getdatestr:=L0(Year)+'/'+L0(Month)+'/'+L0(Day);
  340. end;
  341. function filetimestring( t : longint) : string;
  342. {
  343. convert dos datetime t to a string YY/MM/DD HH:MM:SS
  344. }
  345. var
  346. DT : DateTime;
  347. begin
  348. if t=-1 then
  349. begin
  350. FileTimeString:='Not Found';
  351. exit;
  352. end;
  353. unpacktime(t,DT);
  354. filetimestring:=L0(dt.Year)+'/'+L0(dt.Month)+'/'+L0(dt.Day)+' '+L0(dt.Hour)+':'+L0(dt.min)+':'+L0(dt.sec);
  355. end;
  356. {****************************************************************************
  357. Default Macro Handling
  358. ****************************************************************************}
  359. procedure DefaultReplacements(var s:string);
  360. begin
  361. { Replace some macro's }
  362. Replace(s,'$FPCVER',version_string);
  363. Replace(s,'$VERSION',version_string);
  364. Replace(s,'$FULLVERSION',full_version_string);
  365. Replace(s,'$FPCDATE',date_string);
  366. Replace(s,'$FPCTARGET',target_cpu_string);
  367. Replace(s,'$FPCCPU',target_cpu_string);
  368. Replace(s,'$TARGET',target_path);
  369. Replace(s,'$FPCOS',target_path);
  370. end;
  371. {****************************************************************************
  372. File Handling
  373. ****************************************************************************}
  374. function GetCurrentDir:string;
  375. var
  376. CurrentDir : string;
  377. begin
  378. GetDir(0,CurrentDir);
  379. GetCurrentDir:=FixPath(CurrentDir,false);
  380. end;
  381. function path_absolute(const s : string) : boolean;
  382. {
  383. is path s an absolute path?
  384. }
  385. begin
  386. path_absolute:=false;
  387. {$ifdef unix}
  388. if (length(s)>0) and (s[1]='/') then
  389. path_absolute:=true;
  390. {$else unix}
  391. {$ifdef amiga}
  392. if ((length(s)>0) and ((s[1]='\') or (s[1]='/'))) or (Pos(':',s) = length(s)) then
  393. path_absolute:=true;
  394. {$else}
  395. if ((length(s)>0) and ((s[1]='\') or (s[1]='/'))) or
  396. ((length(s)>2) and (s[2]=':') and ((s[3]='\') or (s[3]='/'))) then
  397. path_absolute:=true;
  398. {$endif amiga}
  399. {$endif unix}
  400. end;
  401. {$ifndef FPC}
  402. Procedure FindClose(var Info : SearchRec);
  403. Begin
  404. End;
  405. {$endif not FPC}
  406. Function FileExists ( Const F : String) : Boolean;
  407. Var
  408. res : boolean;
  409. {$ifndef delphi}
  410. Info : SearchRec;
  411. {$endif}
  412. begin
  413. {$ifdef delphi}
  414. res:=sysutils.FileExists(f);
  415. {$else}
  416. findfirst(F,readonly+archive+hidden,info);
  417. res:=(doserror=0);
  418. findclose(Info);
  419. {$endif delphi}
  420. if assigned({$ifndef FPCPROVCAR}@{$endif}do_comment) then
  421. begin
  422. if res then
  423. do_comment(V_Tried,'Searching file '+F+'... found')
  424. else
  425. do_comment(V_Tried,'Searching file '+F+'... not found');
  426. end;
  427. FileExists:=res;
  428. end;
  429. Function PathExists ( F : String) : Boolean;
  430. Var
  431. Info : SearchRec;
  432. disk : byte;
  433. begin
  434. { these operating systems have dos type drives }
  435. if source_info.system in [system_m68k_atari,system_i386_go32v2,
  436. system_i386_win32,system_i386_os2,
  437. system_i386_emx,system_i386_wdosx] then
  438. Begin
  439. if (Length(f)=3) and (F[2]=':') and (F[3] in ['/','\']) then
  440. begin
  441. if F[1] in ['A'..'Z'] then
  442. disk:=ord(F[1])-ord('A')+1
  443. else if F[1] in ['a'..'z'] then
  444. disk:=ord(F[1])-ord('a')+1
  445. else
  446. disk:=255;
  447. if disk=255 then
  448. PathExists:=false
  449. else
  450. PathExists:=(DiskSize(disk)<>-1);
  451. exit;
  452. end;
  453. end;
  454. if F[Length(f)] in ['/','\'] then
  455. Delete(f,length(f),1);
  456. findfirst(F,readonly+archive+hidden+directory,info);
  457. PathExists:=(doserror=0) and ((info.attr and directory)=directory);
  458. findclose(Info);
  459. end;
  460. Function RemoveFile(const f:string):boolean;
  461. var
  462. g : file;
  463. begin
  464. assign(g,f);
  465. {$I-}
  466. erase(g);
  467. {$I+}
  468. RemoveFile:=(ioresult=0);
  469. end;
  470. Function RemoveDir(d:string):boolean;
  471. begin
  472. if d[length(d)]=source_info.DirSep then
  473. Delete(d,length(d),1);
  474. {$I-}
  475. rmdir(d);
  476. {$I+}
  477. RemoveDir:=(ioresult=0);
  478. end;
  479. Function SplitPath(const s:string):string;
  480. var
  481. i : longint;
  482. begin
  483. i:=Length(s);
  484. while (i>0) and not(s[i] in ['/','\']) do
  485. dec(i);
  486. SplitPath:=Copy(s,1,i);
  487. end;
  488. Function SplitFileName(const s:string):string;
  489. var
  490. p : dirstr;
  491. n : namestr;
  492. e : extstr;
  493. begin
  494. FSplit(s,p,n,e);
  495. SplitFileName:=n+e;
  496. end;
  497. Function SplitName(const s:string):string;
  498. var
  499. i,j : longint;
  500. begin
  501. i:=Length(s);
  502. j:=Length(s);
  503. while (i>0) and not(s[i] in ['/','\']) do
  504. dec(i);
  505. while (j>0) and (s[j]<>'.') do
  506. dec(j);
  507. if j<=i then
  508. j:=255;
  509. SplitName:=Copy(s,i+1,j-(i+1));
  510. end;
  511. Function SplitExtension(Const HStr:String):String;
  512. var
  513. j : longint;
  514. begin
  515. j:=length(Hstr);
  516. while (j>0) and (Hstr[j]<>'.') do
  517. begin
  518. if hstr[j]=source_info.DirSep then
  519. j:=0
  520. else
  521. dec(j);
  522. end;
  523. if j=0 then
  524. j:=254;
  525. SplitExtension:=Copy(Hstr,j,255);
  526. end;
  527. Function AddExtension(Const HStr,ext:String):String;
  528. begin
  529. if (Ext<>'') and (SplitExtension(HStr)='') then
  530. AddExtension:=Hstr+Ext
  531. else
  532. AddExtension:=Hstr;
  533. end;
  534. Function ForceExtension(Const HStr,ext:String):String;
  535. var
  536. j : longint;
  537. begin
  538. j:=length(Hstr);
  539. while (j>0) and (Hstr[j]<>'.') do
  540. dec(j);
  541. if j=0 then
  542. j:=255;
  543. ForceExtension:=Copy(Hstr,1,j-1)+Ext;
  544. end;
  545. Function FixPath(s:string;allowdot:boolean):string;
  546. var
  547. i : longint;
  548. begin
  549. { Fix separator }
  550. for i:=1 to length(s) do
  551. if s[i] in ['/','\'] then
  552. s[i]:=source_info.DirSep;
  553. { Fix ending / }
  554. if (length(s)>0) and (s[length(s)]<>source_info.DirSep) and
  555. (s[length(s)]<>':') then
  556. s:=s+source_info.DirSep;
  557. { Remove ./ }
  558. if (not allowdot) and (s='.'+source_info.DirSep) then
  559. s:='';
  560. { return }
  561. if source_info.files_case_relevent then
  562. FixPath:=s
  563. else
  564. FixPath:=Lower(s);
  565. end;
  566. function FixFileName(const s:string):string;
  567. var
  568. i : longint;
  569. begin
  570. if source_info.files_case_relevent then
  571. begin
  572. for i:=1 to length(s) do
  573. begin
  574. case s[i] of
  575. '/','\' :
  576. FixFileName[i]:=source_info.dirsep;
  577. else
  578. FixFileName[i]:=s[i];
  579. end;
  580. end;
  581. end
  582. else
  583. begin
  584. for i:=1 to length(s) do
  585. begin
  586. case s[i] of
  587. '/','\' :
  588. FixFileName[i]:=source_info.dirsep;
  589. 'A'..'Z' :
  590. FixFileName[i]:=char(byte(s[i])+32);
  591. else
  592. FixFileName[i]:=s[i];
  593. end;
  594. end;
  595. end;
  596. FixFileName[0]:=s[0];
  597. end;
  598. Function TargetFixPath(s:string;allowdot:boolean):string;
  599. var
  600. i : longint;
  601. begin
  602. { Fix separator }
  603. for i:=1 to length(s) do
  604. if s[i] in ['/','\'] then
  605. s[i]:=target_info.DirSep;
  606. { Fix ending / }
  607. if (length(s)>0) and (s[length(s)]<>target_info.DirSep) and
  608. (s[length(s)]<>':') then
  609. s:=s+target_info.DirSep;
  610. { Remove ./ }
  611. if (not allowdot) and (s='.'+target_info.DirSep) then
  612. s:='';
  613. { return }
  614. if target_info.files_case_relevent then
  615. TargetFixPath:=s
  616. else
  617. TargetFixPath:=Lower(s);
  618. end;
  619. function TargetFixFileName(const s:string):string;
  620. var
  621. i : longint;
  622. begin
  623. if target_info.files_case_relevent then
  624. begin
  625. for i:=1 to length(s) do
  626. begin
  627. case s[i] of
  628. '/','\' :
  629. TargetFixFileName[i]:=target_info.dirsep;
  630. else
  631. TargetFixFileName[i]:=s[i];
  632. end;
  633. end;
  634. end
  635. else
  636. begin
  637. for i:=1 to length(s) do
  638. begin
  639. case s[i] of
  640. '/','\' :
  641. TargetFixFileName[i]:=target_info.dirsep;
  642. 'A'..'Z' :
  643. TargetFixFileName[i]:=char(byte(s[i])+32);
  644. else
  645. TargetFixFileName[i]:=s[i];
  646. end;
  647. end;
  648. end;
  649. TargetFixFileName[0]:=s[0];
  650. end;
  651. procedure SplitBinCmd(const s:string;var bstr,cstr:string);
  652. var
  653. i : longint;
  654. begin
  655. i:=pos(' ',s);
  656. if i>0 then
  657. begin
  658. bstr:=Copy(s,1,i-1);
  659. cstr:=Copy(s,i+1,length(s)-i);
  660. end
  661. else
  662. begin
  663. bstr:=s;
  664. cstr:='';
  665. end;
  666. end;
  667. procedure TSearchPathList.AddPath(s:string;addfirst:boolean);
  668. begin
  669. AddPath('',s,AddFirst);
  670. end;
  671. procedure TSearchPathList.AddPath(SrcPath,s:string;addfirst:boolean);
  672. var
  673. j : longint;
  674. hs,hsd,
  675. CurrentDir,
  676. CurrPath : string;
  677. subdirfound : boolean;
  678. dir : searchrec;
  679. hp : TStringListItem;
  680. procedure addcurrpath;
  681. begin
  682. if addfirst then
  683. begin
  684. Remove(currPath);
  685. Insert(currPath);
  686. end
  687. else
  688. begin
  689. { Check if already in path, then we don't add it }
  690. hp:=Find(currPath);
  691. if not assigned(hp) then
  692. Concat(currPath);
  693. end;
  694. end;
  695. begin
  696. if s='' then
  697. exit;
  698. { Support default macro's }
  699. DefaultReplacements(s);
  700. { get current dir }
  701. CurrentDir:=GetCurrentDir;
  702. repeat
  703. { get currpath }
  704. if addfirst then
  705. begin
  706. j:=length(s);
  707. while (j>0) and (s[j]<>';') do
  708. dec(j);
  709. CurrPath:=FixPath(Copy(s,j+1,length(s)-j),false);
  710. if j=0 then
  711. s:=''
  712. else
  713. System.Delete(s,j,length(s)-j+1);
  714. end
  715. else
  716. begin
  717. j:=Pos(';',s);
  718. if j=0 then
  719. j:=255;
  720. CurrPath:=SrcPath+FixPath(Copy(s,1,j-1),false);
  721. System.Delete(s,1,j);
  722. end;
  723. { fix pathname }
  724. if CurrPath='' then
  725. CurrPath:='.'+source_info.DirSep
  726. else
  727. begin
  728. CurrPath:=FixPath(FExpand(CurrPath),false);
  729. if (CurrentDir<>'') and (Copy(CurrPath,1,length(CurrentDir))=CurrentDir) then
  730. begin
  731. {$ifdef AMIGA}
  732. CurrPath:=CurrentDir+Copy(CurrPath,length(CurrentDir)+1,255);
  733. {$else}
  734. CurrPath:='.'+source_info.DirSep+Copy(CurrPath,length(CurrentDir)+1,255);
  735. {$endif}
  736. end;
  737. end;
  738. { wildcard adding ? }
  739. if pos('*',currpath)>0 then
  740. begin
  741. if currpath[length(currpath)]=source_info.dirsep then
  742. hs:=Copy(currpath,1,length(CurrPath)-1)
  743. else
  744. hs:=currpath;
  745. hsd:=SplitPath(hs);
  746. findfirst(hs,directory,dir);
  747. subdirfound:=false;
  748. while doserror=0 do
  749. begin
  750. if (dir.name<>'.') and
  751. (dir.name<>'..') and
  752. ((dir.attr and directory)<>0) then
  753. begin
  754. subdirfound:=true;
  755. currpath:=hsd+dir.name+source_info.dirsep;
  756. hp:=Find(currPath);
  757. if not assigned(hp) then
  758. AddCurrPath;
  759. end;
  760. findnext(dir);
  761. if not subdirfound then
  762. WarnNonExistingPath(currpath);
  763. end;
  764. FindClose(dir);
  765. end
  766. else
  767. begin
  768. if PathExists(currpath) then
  769. addcurrpath
  770. else
  771. WarnNonExistingPath(currpath);
  772. end;
  773. until (s='');
  774. end;
  775. procedure TSearchPathList.AddList(list:TSearchPathList;addfirst:boolean);
  776. var
  777. s : string;
  778. hl : TSearchPathList;
  779. hp,hp2 : TStringListItem;
  780. begin
  781. if list.empty then
  782. exit;
  783. { create temp and reverse the list }
  784. if addfirst then
  785. begin
  786. hl:=TSearchPathList.Create;
  787. hp:=TStringListItem(list.first);
  788. while assigned(hp) do
  789. begin
  790. hl.insert(hp.Str);
  791. hp:=TStringListItem(hp.next);
  792. end;
  793. while not hl.empty do
  794. begin
  795. s:=hl.GetFirst;
  796. Remove(s);
  797. Insert(s);
  798. end;
  799. hl.Free;
  800. end
  801. else
  802. begin
  803. hp:=TStringListItem(list.first);
  804. while assigned(hp) do
  805. begin
  806. hp2:=Find(hp.Str);
  807. { Check if already in path, then we don't add it }
  808. if not assigned(hp2) then
  809. Concat(hp.Str);
  810. hp:=TStringListItem(hp.next);
  811. end;
  812. end;
  813. end;
  814. function TSearchPathList.FindFile(const f : string;var foundfile:string):boolean;
  815. Var
  816. p : TStringListItem;
  817. begin
  818. FindFile:=false;
  819. p:=TStringListItem(first);
  820. while assigned(p) do
  821. begin
  822. {
  823. Search order for case sensitive systems:
  824. 1. lowercase
  825. 2. NormalCase
  826. 3. UPPERCASE
  827. None case sensitive only lowercase
  828. }
  829. FoundFile:=p.Str+Lower(f);
  830. If FileExists(FoundFile) then
  831. begin
  832. FindFile:=true;
  833. exit;
  834. end;
  835. {$ifdef UNIX}
  836. FoundFile:=p.Str+f;
  837. If FileExists(FoundFile) then
  838. begin
  839. FindFile:=true;
  840. exit;
  841. end;
  842. FoundFile:=p.Str+Upper(f);
  843. If FileExists(FoundFile) then
  844. begin
  845. FindFile:=true;
  846. exit;
  847. end;
  848. {$endif UNIX}
  849. p:=TStringListItem(p.next);
  850. end;
  851. { Return original filename if not found }
  852. FoundFile:=f;
  853. end;
  854. Function GetFileTime ( Var F : File) : Longint;
  855. Var
  856. {$ifdef hasunix}
  857. info: Stat;
  858. {$endif}
  859. L : longint;
  860. begin
  861. {$ifdef hasunix}
  862. {$IFDEF VER1_0}
  863. FStat (F,Info);
  864. {$ifdef BSD}
  865. L:=Info.st_Mtime;
  866. {$else}
  867. L:=Info.Mtime;
  868. {$endif}
  869. {$ELSE}
  870. FPFStat (F,Info);
  871. L:=Info.st_Mtime;
  872. {$ENDIF}
  873. {$else}
  874. GetFTime(f,l);
  875. {$endif}
  876. GetFileTime:=L;
  877. end;
  878. Function GetNamedFileTime (Const F : String) : Longint;
  879. begin
  880. GetNamedFileTime:=do_getnamedfiletime(F);
  881. end;
  882. function FindFile(const f : string;path : string;var foundfile:string):boolean;
  883. Var
  884. singlepathstring : string;
  885. i : longint;
  886. begin
  887. {$ifdef Unix}
  888. for i:=1 to length(path) do
  889. if path[i]=':' then
  890. path[i]:=';';
  891. {$endif Unix}
  892. FindFile:=false;
  893. repeat
  894. i:=pos(';',path);
  895. if i=0 then
  896. i:=256;
  897. singlepathstring:=FixPath(copy(path,1,i-1),false);
  898. delete(path,1,i);
  899. {
  900. Search order for case sensitive systems:
  901. 1. lowercase
  902. 2. NormalCase
  903. 3. UPPERCASE
  904. None case sensitive only lowercase
  905. }
  906. FoundFile:=singlepathstring+Lower(f);
  907. If FileExists(FoundFile) then
  908. begin
  909. FindFile:=true;
  910. exit;
  911. end;
  912. {$ifdef UNIX}
  913. FoundFile:=singlepathstring+f;
  914. If FileExists(FoundFile) then
  915. begin
  916. FindFile:=true;
  917. exit;
  918. end;
  919. FoundFile:=singlepathstring+Upper(f);
  920. If FileExists(FoundFile) then
  921. begin
  922. FindFile:=true;
  923. exit;
  924. end;
  925. {$endif UNIX}
  926. until path='';
  927. FoundFile:=f;
  928. end;
  929. function FindFilePchar(const f : string;path : pchar;var foundfile:string):boolean;
  930. Var
  931. singlepathstring : string;
  932. startpc,pc : pchar;
  933. sepch : char;
  934. begin
  935. {$ifdef Unix}
  936. sepch:=':';
  937. {$else}
  938. sepch:=';';
  939. {$endif Unix}
  940. FindFilePchar:=false;
  941. pc:=path;
  942. repeat
  943. startpc:=pc;
  944. while (pc^<>sepch) and (pc^<>';') and (pc^<>#0) do
  945. inc(pc);
  946. move(startpc^,singlepathstring[1],pc-startpc);
  947. singlepathstring[0]:=char(longint(pc-startpc));
  948. singlepathstring:=FixPath(singlepathstring,false);
  949. {
  950. Search order for case sensitive systems:
  951. 1. lowercase
  952. 2. NormalCase
  953. 3. UPPERCASE
  954. None case sensitive only lowercase
  955. }
  956. FoundFile:=singlepathstring+Lower(f);
  957. If FileExists(FoundFile) then
  958. begin
  959. FindFilePchar:=true;
  960. exit;
  961. end;
  962. {$ifdef UNIX}
  963. FoundFile:=singlepathstring+f;
  964. If FileExists(FoundFile) then
  965. begin
  966. FindFilePchar:=true;
  967. exit;
  968. end;
  969. FoundFile:=singlepathstring+Upper(f);
  970. If FileExists(FoundFile) then
  971. begin
  972. FindFilePchar:=true;
  973. exit;
  974. end;
  975. {$endif UNIX}
  976. if (pc^=#0) then
  977. break;
  978. inc(pc);
  979. until false;
  980. end;
  981. function FindExe(const bin:string;var foundfile:string):boolean;
  982. var
  983. p : pchar;
  984. found : boolean;
  985. begin
  986. found:=FindFile(FixFileName(AddExtension(bin,source_info.exeext)),'.;'+exepath,foundfile);
  987. if not found then
  988. begin
  989. p:=GetEnvPchar('PATH');
  990. found:=FindFilePChar(FixFileName(AddExtension(bin,source_info.exeext)),p,foundfile);
  991. FreeEnvPChar(p);
  992. end;
  993. FindExe:=found;
  994. end;
  995. function GetShortName(const n:string):string;
  996. {$ifdef win32}
  997. var
  998. hs,hs2 : string;
  999. i : longint;
  1000. {$endif}
  1001. {$ifdef go32v2}
  1002. var
  1003. hs : string;
  1004. {$endif}
  1005. {$ifdef watcom}
  1006. var
  1007. hs : string;
  1008. {$endif}
  1009. begin
  1010. GetShortName:=n;
  1011. {$ifdef win32}
  1012. hs:=n+#0;
  1013. i:=Windows.GetShortPathName(@hs[1],@hs2[1],high(hs2));
  1014. if (i>0) and (i<=high(hs2)) then
  1015. begin
  1016. hs2[0]:=chr(strlen(@hs2[1]));
  1017. GetShortName:=hs2;
  1018. end;
  1019. {$endif}
  1020. {$ifdef go32v2}
  1021. hs:=n;
  1022. if Dos.GetShortName(hs) then
  1023. GetShortName:=hs;
  1024. {$endif}
  1025. {$ifdef watcom}
  1026. hs:=n;
  1027. if Dos.GetShortName(hs) then
  1028. GetShortName:=hs;
  1029. {$endif}
  1030. end;
  1031. {****************************************************************************
  1032. OS Dependent things
  1033. ****************************************************************************}
  1034. function GetEnvPChar(const envname:string):pchar;
  1035. {$ifdef win32}
  1036. var
  1037. s : string;
  1038. i,len : longint;
  1039. hp,p,p2 : pchar;
  1040. {$endif}
  1041. begin
  1042. {$ifdef hasunix}
  1043. GetEnvPchar:={$ifdef ver1_0}Linux.getenv{$else}BaseUnix.fpGetEnv{$endif}(envname);
  1044. {$define GETENVOK}
  1045. {$endif}
  1046. {$ifdef win32}
  1047. GetEnvPchar:=nil;
  1048. p:=GetEnvironmentStrings;
  1049. hp:=p;
  1050. while hp^<>#0 do
  1051. begin
  1052. s:=strpas(hp);
  1053. i:=pos('=',s);
  1054. len:=strlen(hp);
  1055. if upper(copy(s,1,i-1))=upper(envname) then
  1056. begin
  1057. GetMem(p2,len-length(envname));
  1058. Move(hp[i],p2^,len-length(envname));
  1059. GetEnvPchar:=p2;
  1060. break;
  1061. end;
  1062. { next string entry}
  1063. hp:=hp+len+1;
  1064. end;
  1065. FreeEnvironmentStrings(p);
  1066. {$define GETENVOK}
  1067. {$endif}
  1068. {$ifdef os2}
  1069. GetEnvPChar := Dos.GetEnvPChar (EnvName);
  1070. {$define GETENVOK}
  1071. {$endif}
  1072. {$ifdef GETENVOK}
  1073. {$undef GETENVOK}
  1074. {$else}
  1075. GetEnvPchar:=StrPNew({$ifdef delphi}DMisc{$else}Dos{$endif}.Getenv(envname));
  1076. {$endif}
  1077. end;
  1078. procedure FreeEnvPChar(p:pchar);
  1079. begin
  1080. {$ifndef hasunix}
  1081. {$ifndef os2}
  1082. StrDispose(p);
  1083. {$endif}
  1084. {$endif}
  1085. end;
  1086. Procedure Shell(const command:string);
  1087. { This is already defined in the linux.ppu for linux, need for the *
  1088. expansion under linux }
  1089. {$ifdef hasunix}
  1090. begin
  1091. {$ifdef ver1_0}Linux{$else}Unix{$endif}.Shell(command);
  1092. end;
  1093. {$else}
  1094. {$ifdef amiga}
  1095. begin
  1096. exec('',command);
  1097. end;
  1098. {$else}
  1099. var
  1100. comspec : string;
  1101. begin
  1102. comspec:=getenv('COMSPEC');
  1103. Exec(comspec,' /C '+command);
  1104. end;
  1105. {$endif}
  1106. {$endif}
  1107. {$ifdef CPUI386}
  1108. { later, this should be replaced by the math unit }
  1109. const
  1110. Default8087CW : word = $1332;
  1111. {$ASMMODE ATT}
  1112. procedure Set8087CW(cw:word);assembler;
  1113. asm
  1114. movw cw,%ax
  1115. movw %ax,default8087cw
  1116. fnclex
  1117. fldcw default8087cw
  1118. end;
  1119. function Get8087CW:word;assembler;
  1120. asm
  1121. pushl $0
  1122. fnstcw (%esp)
  1123. popl %eax
  1124. end;
  1125. function SetFPUExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  1126. var
  1127. CtlWord: Word;
  1128. begin
  1129. CtlWord:=Get8087CW;
  1130. Set8087CW( (CtlWord and $FFC0) or Byte(Longint(Mask)) );
  1131. Result:=TFPUExceptionMask(CtlWord and $3F);
  1132. end;
  1133. {$else CPUI386}
  1134. function SetFPUExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  1135. begin
  1136. end;
  1137. {$endif CPUI386}
  1138. function is_number_float(d : double) : boolean;
  1139. var
  1140. bytearray : array[0..7] of byte;
  1141. begin
  1142. move(d,bytearray,8);
  1143. { only 1.1 save, 1.0.x will use always little endian }
  1144. {$ifdef FPC_BIG_ENDIAN}
  1145. result:=((bytearray[0] and $7f)<>$7f) or ((bytearray[1] and $f0)<>$f0);
  1146. {$else FPC_BIG_ENDIAN}
  1147. result:=((bytearray[7] and $7f)<>$7f) or ((bytearray[6] and $f0)<>$f0);
  1148. {$endif FPC_BIG_ENDIAN}
  1149. end;
  1150. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  1151. var
  1152. b : boolean;
  1153. begin
  1154. b:=true;
  1155. if s='DEFAULT' then
  1156. aktmodeswitches:=initmodeswitches
  1157. else
  1158. if s='DELPHI' then
  1159. aktmodeswitches:=delphimodeswitches
  1160. else
  1161. if s='TP' then
  1162. aktmodeswitches:=tpmodeswitches
  1163. else
  1164. if s='FPC' then
  1165. aktmodeswitches:=fpcmodeswitches
  1166. else
  1167. if s='OBJFPC' then
  1168. aktmodeswitches:=objfpcmodeswitches
  1169. else
  1170. if s='GPC' then
  1171. aktmodeswitches:=gpcmodeswitches
  1172. else
  1173. if s='MAC' then
  1174. aktmodeswitches:=macmodeswitches
  1175. else
  1176. b:=false;
  1177. if b and changeInit then
  1178. initmodeswitches := aktmodeswitches;
  1179. if b then
  1180. begin
  1181. { turn ansistrings on by default ? }
  1182. if (m_delphi in aktmodeswitches) then
  1183. begin
  1184. include(aktlocalswitches,cs_ansistrings);
  1185. if changeinit then
  1186. include(initlocalswitches,cs_ansistrings);
  1187. end
  1188. else
  1189. begin
  1190. exclude(aktlocalswitches,cs_ansistrings);
  1191. if changeinit then
  1192. exclude(initlocalswitches,cs_ansistrings);
  1193. end;
  1194. { enum packing }
  1195. if (m_tp7 in aktmodeswitches) then
  1196. aktpackenum:=1
  1197. else
  1198. aktpackenum:=4;
  1199. if changeinit then
  1200. initpackenum:=aktpackenum;
  1201. {$ifdef i386}
  1202. { Default to intel assembler for delphi/tp7 on i386 }
  1203. if (m_delphi in aktmodeswitches) or
  1204. (m_tp7 in aktmodeswitches) then
  1205. aktasmmode:=asmmode_i386_intel;
  1206. if changeinit then
  1207. initasmmode:=aktasmmode;
  1208. {$endif i386}
  1209. end;
  1210. SetCompileMode:=b;
  1211. end;
  1212. function SetAktProcCall(const s:string; changeInit:boolean):boolean;
  1213. const
  1214. DefProcCallName : array[tproccalloption] of string[12] = ('',
  1215. 'CDECL',
  1216. 'CPPDECL',
  1217. '', { compilerproc }
  1218. 'FAR16',
  1219. 'FPCCALL',
  1220. 'INLINE',
  1221. '', { internproc }
  1222. '', { palmossyscall }
  1223. 'PASCAL',
  1224. 'REGISTER',
  1225. 'SAFECALL',
  1226. 'STDCALL'
  1227. );
  1228. var
  1229. t : tproccalloption;
  1230. begin
  1231. SetAktProcCall:=false;
  1232. for t:=low(tproccalloption) to high(tproccalloption) do
  1233. if DefProcCallName[t]=s then
  1234. begin
  1235. AktDefProcCall:=t;
  1236. SetAktProcCall:=true;
  1237. break;
  1238. end;
  1239. if changeinit then
  1240. InitDefProcCall:=AktDefProcCall;
  1241. end;
  1242. { '('D1:'00000000-'D2:'0000-'D3:'0000-'D4:'0000-000000000000)' }
  1243. function string2guid(const s: string; var GUID: TGUID): boolean;
  1244. function ishexstr(const hs: string): boolean;
  1245. var
  1246. i: integer;
  1247. begin
  1248. ishexstr:=false;
  1249. for i:=1 to Length(hs) do begin
  1250. if not (hs[i] in ['0'..'9','A'..'F','a'..'f']) then
  1251. exit;
  1252. end;
  1253. ishexstr:=true;
  1254. end;
  1255. function hexstr2longint(const hexs: string): longint;
  1256. var
  1257. i: integer;
  1258. rl: longint;
  1259. begin
  1260. rl:=0;
  1261. for i:=1 to length(hexs) do begin
  1262. rl:=rl shl 4;
  1263. case hexs[i] of
  1264. '0'..'9' : inc(rl,ord(hexs[i])-ord('0'));
  1265. 'A'..'F' : inc(rl,ord(hexs[i])-ord('A')+10);
  1266. 'a'..'f' : inc(rl,ord(hexs[i])-ord('a')+10);
  1267. end
  1268. end;
  1269. hexstr2longint:=rl;
  1270. end;
  1271. var
  1272. i: integer;
  1273. begin
  1274. if (Length(s)=38) and (s[1]='{') and (s[38]='}') and
  1275. (s[10]='-') and (s[15]='-') and (s[20]='-') and (s[25]='-') and
  1276. ishexstr(copy(s,2,8)) and ishexstr(copy(s,11,4)) and
  1277. ishexstr(copy(s,16,4)) and ishexstr(copy(s,21,4)) and
  1278. ishexstr(copy(s,26,12)) then begin
  1279. GUID.D1:=dword(hexstr2longint(copy(s,2,8)));
  1280. { these values are arealdy in the correct range (4 chars = word) }
  1281. GUID.D2:=word(hexstr2longint(copy(s,11,4)));
  1282. GUID.D3:=word(hexstr2longint(copy(s,16,4)));
  1283. for i:=0 to 1 do
  1284. GUID.D4[i]:=byte(hexstr2longint(copy(s,21+i*2,2)));
  1285. for i:=2 to 7 do
  1286. GUID.D4[i]:=byte(hexstr2longint(copy(s,22+i*2,2)));
  1287. string2guid:=true;
  1288. end
  1289. else
  1290. string2guid:=false;
  1291. end;
  1292. function guid2string(const GUID: TGUID): string;
  1293. function long2hex(l, len: longint): string;
  1294. const
  1295. hextbl: array[0..15] of char = '0123456789ABCDEF';
  1296. var
  1297. rs: string;
  1298. i: integer;
  1299. begin
  1300. rs[0]:=chr(len);
  1301. for i:=len downto 1 do begin
  1302. rs[i]:=hextbl[l and $F];
  1303. l:=l shr 4;
  1304. end;
  1305. long2hex:=rs;
  1306. end;
  1307. begin
  1308. guid2string:=
  1309. '{'+long2hex(GUID.D1,8)+
  1310. '-'+long2hex(GUID.D2,4)+
  1311. '-'+long2hex(GUID.D3,4)+
  1312. '-'+long2hex(GUID.D4[0],2)+long2hex(GUID.D4[1],2)+
  1313. '-'+long2hex(GUID.D4[2],2)+long2hex(GUID.D4[3],2)+
  1314. long2hex(GUID.D4[4],2)+long2hex(GUID.D4[5],2)+
  1315. long2hex(GUID.D4[6],2)+long2hex(GUID.D4[7],2)+
  1316. '}';
  1317. end;
  1318. procedure swap_qword(var q : qword);
  1319. begin
  1320. q:=(qword(lo(q)) shl 32) or hi(q);
  1321. end;
  1322. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  1323. var
  1324. tok : string;
  1325. vstr : string;
  1326. l : longint;
  1327. code : integer;
  1328. b : talignmentinfo;
  1329. begin
  1330. UpdateAlignmentStr:=true;
  1331. uppervar(s);
  1332. fillchar(b,sizeof(b),0);
  1333. repeat
  1334. tok:=GetToken(s,'=');
  1335. if tok='' then
  1336. break;
  1337. vstr:=GetToken(s,',');
  1338. val(vstr,l,code);
  1339. if tok='PROC' then
  1340. b.procalign:=l
  1341. else if tok='JUMP' then
  1342. b.jumpalign:=l
  1343. else if tok='LOOP' then
  1344. b.loopalign:=l
  1345. else if tok='CONSTMIN' then
  1346. b.constalignmin:=l
  1347. else if tok='CONSTMAX' then
  1348. b.constalignmax:=l
  1349. else if tok='VARMIN' then
  1350. b.varalignmin:=l
  1351. else if tok='VARMAX' then
  1352. b.varalignmax:=l
  1353. else if tok='LOCALMIN' then
  1354. b.localalignmin:=l
  1355. else if tok='LOCALMAX' then
  1356. b.localalignmax:=l
  1357. else if tok='RECORDMIN' then
  1358. b.recordalignmin:=l
  1359. else if tok='RECORDMAX' then
  1360. b.recordalignmax:=l
  1361. else { Error }
  1362. UpdateAlignmentStr:=false;
  1363. until false;
  1364. UpdateAlignment(a,b);
  1365. end;
  1366. function var_align(siz: longint): longint;
  1367. begin
  1368. siz := size_2_align(siz);
  1369. var_align := used_align(siz,aktalignment.varalignmin,aktalignment.varalignmax);
  1370. end;
  1371. function const_align(siz: longint): longint;
  1372. begin
  1373. siz := size_2_align(siz);
  1374. const_align := used_align(siz,aktalignment.constalignmin,aktalignment.constalignmax);
  1375. end;
  1376. {****************************************************************************
  1377. Init
  1378. ****************************************************************************}
  1379. {$ifdef unix}
  1380. {$define need_path_search}
  1381. {$endif unix}
  1382. {$ifdef os2}
  1383. {$define need_path_search}
  1384. {$endif os2}
  1385. procedure get_exepath;
  1386. var
  1387. hs1 : namestr;
  1388. hs2 : extstr;
  1389. p : pchar;
  1390. begin
  1391. {$ifdef delphi}
  1392. exepath:=dmisc.getenv('PPC_EXEC_PATH');
  1393. {$else delphi}
  1394. exepath:=dos.getenv('PPC_EXEC_PATH');
  1395. {$endif delphi}
  1396. if exepath='' then
  1397. fsplit(FixFileName(system.paramstr(0)),exepath,hs1,hs2);
  1398. {$ifdef need_path_search}
  1399. if exepath='' then
  1400. begin
  1401. if pos(source_info.exeext,hs1) <>
  1402. (length(hs1) - length(source_info.exeext)+1) then
  1403. hs1 := hs1 + source_info.exeext;
  1404. p:=GetEnvPchar('PATH');
  1405. FindFilePChar(hs1,p,exepath);
  1406. FreeEnvPChar(p);
  1407. exepath:=SplitPath(exepath);
  1408. end;
  1409. {$endif need_path_search}
  1410. exepath:=FixPath(exepath,false);
  1411. end;
  1412. procedure DoneGlobals;
  1413. begin
  1414. initdefines.free;
  1415. if assigned(DLLImageBase) then
  1416. StringDispose(DLLImageBase);
  1417. RelocSection:=true;
  1418. RelocSectionSetExplicitly:=false;
  1419. UseDeffileForExport:=true;
  1420. librarysearchpath.Free;
  1421. unitsearchpath.Free;
  1422. objectsearchpath.Free;
  1423. includesearchpath.Free;
  1424. end;
  1425. procedure InitGlobals;
  1426. begin
  1427. get_exepath;
  1428. { reset globals }
  1429. do_build:=false;
  1430. do_release:=false;
  1431. do_make:=true;
  1432. compile_level:=0;
  1433. DLLsource:=false;
  1434. inlining_procedure:=false;
  1435. resolving_forward:=false;
  1436. make_ref:=false;
  1437. { Output }
  1438. OutputFile:='';
  1439. OutputExeDir:='';
  1440. OutputUnitDir:='';
  1441. { Utils directory }
  1442. utilsdirectory:='';
  1443. utilsprefix:='';
  1444. { Search Paths }
  1445. librarysearchpath:=TSearchPathList.Create;
  1446. unitsearchpath:=TSearchPathList.Create;
  1447. includesearchpath:=TSearchPathList.Create;
  1448. objectsearchpath:=TSearchPathList.Create;
  1449. { Def file }
  1450. usewindowapi:=false;
  1451. description:='Compiled by FPC '+version_string+' - '+target_cpu_string;
  1452. dllversion:='';
  1453. nwscreenname := '';
  1454. nwthreadname := '';
  1455. nwcopyright := '';
  1456. { Init values }
  1457. initmodeswitches:=fpcmodeswitches;
  1458. initlocalswitches:=[cs_check_io,cs_typed_const_writable];
  1459. initmoduleswitches:=[cs_extsyntax,cs_implicit_exceptions];
  1460. initsourcecodepage:='8859-1';
  1461. initglobalswitches:=[cs_check_unit_name,cs_link_static{$ifdef INTERNALLINKER},cs_link_internal,cs_link_map{$endif}];
  1462. initoutputformat:=target_asm.id;
  1463. fillchar(initalignment,sizeof(talignmentinfo),0);
  1464. {$ifdef i386}
  1465. initoptprocessor:=Class386;
  1466. initspecificoptprocessor:=Class386;
  1467. initfputype:=fpu_x87;
  1468. initpackenum:=4;
  1469. {$IFDEF testvarsets}
  1470. initsetalloc:=0;
  1471. {$ENDIF}
  1472. initasmmode:=asmmode_i386_att;
  1473. {$endif i386}
  1474. {$ifdef m68k}
  1475. initoptprocessor:=MC68020;
  1476. initpackenum:=4;
  1477. {$IFDEF testvarsets}
  1478. initsetalloc:=0;
  1479. {$ENDIF}
  1480. initasmmode:=asmmode_standard;
  1481. {$endif m68k}
  1482. {$ifdef powerpc}
  1483. initoptprocessor:=PPC604;
  1484. initpackenum:=4;
  1485. {$IFDEF testvarsets}
  1486. initsetalloc:=0;
  1487. {$ENDIF}
  1488. initasmmode:=asmmode_direct;
  1489. initfputype:=fpu_standard;
  1490. {$endif powerpc}
  1491. {$ifdef sparc}
  1492. initoptprocessor:=SPARC_V8;
  1493. initpackenum:=4;
  1494. {$IFDEF testvarsets}
  1495. initsetalloc:=0;
  1496. {$ENDIF}
  1497. initasmmode:=asmmode_direct;
  1498. {$endif sparc}
  1499. {$ifdef arm}
  1500. initpackenum:=4;
  1501. {$IFDEF testvarsets}
  1502. initsetalloc:=0;
  1503. {$ENDIF}
  1504. initasmmode:=asmmode_direct;
  1505. initfputype:=fpu_fpa;
  1506. {$endif arm}
  1507. {$ifdef x86_64}
  1508. initoptprocessor:=ClassDefault;
  1509. initspecificoptprocessor:=ClassDefault;
  1510. initfputype:=fpu_standard;
  1511. initpackenum:=4;
  1512. {$IFDEF testvarsets}
  1513. initsetalloc:=0;
  1514. {$ENDIF}
  1515. initasmmode:=asmmode_direct;
  1516. {$endif x86_64}
  1517. initinterfacetype:=it_interfacecom;
  1518. initdefproccall:=pocall_default;
  1519. initdefines:=TStringList.Create;
  1520. { memory sizes, will be overriden by parameter or default for target
  1521. in options or init_parser }
  1522. stacksize:=0;
  1523. heapsize:=0;
  1524. apptype:=app_cui;
  1525. end;
  1526. end.
  1527. {
  1528. $Log$
  1529. Revision 1.109 2003-10-11 19:32:04 marco
  1530. * -Xd
  1531. Revision 1.108 2003/10/08 14:10:16 mazen
  1532. + ASMMODE ATT added to bypass fpc.cfg defaults
  1533. Revision 1.107 2003/10/03 22:00:33 peter
  1534. * parameter alignment fixes
  1535. Revision 1.106 2003/10/03 14:16:48 marco
  1536. * -XP<prefix> support
  1537. Revision 1.105 2003/10/02 21:16:18 peter
  1538. * delphi and tp7 mode use intel asm by default
  1539. Revision 1.104 2003/10/01 20:34:48 peter
  1540. * procinfo unit contains tprocinfo
  1541. * cginfo renamed to cgbase
  1542. * moved cgmessage to verbose
  1543. * fixed ppc and sparc compiles
  1544. Revision 1.103 2003/09/24 17:13:22 florian
  1545. + processor type intialization for x86-64 added
  1546. Revision 1.102 2003/09/18 15:38:17 marco
  1547. * BSD 1.0.x still uses st_ prefixes.
  1548. Revision 1.101 2003/09/16 16:17:01 peter
  1549. * varspez in calls to push_addr_param
  1550. Revision 1.100 2003/09/15 20:11:06 marco
  1551. * fixed
  1552. Revision 1.99 2003/09/14 20:26:18 marco
  1553. * Unix reform
  1554. Revision 1.98 2003/09/14 12:55:06 jonas
  1555. * fixed compilation for non-x86
  1556. Revision 1.97 2003/09/07 22:09:34 peter
  1557. * preparations for different default calling conventions
  1558. * various RA fixes
  1559. Revision 1.96 2003/09/06 16:47:24 florian
  1560. + support of NaN and Inf in the compiler as values of real constants
  1561. Revision 1.95 2003/09/05 17:41:12 florian
  1562. * merged Wiktor's Watcom patches in 1.1
  1563. Revision 1.94 2003/09/04 21:37:29 olle
  1564. + added new lagnuage mode: MAC
  1565. Revision 1.93 2003/09/03 11:18:36 florian
  1566. * fixed arm concatcopy
  1567. + arm support in the common compiler sources added
  1568. * moved some generic cg code around
  1569. + tfputype added
  1570. * ...
  1571. Revision 1.92 2003/05/23 22:33:48 florian
  1572. * fix some small flaws which prevent sparc linux system unit from compiling
  1573. * some reformatting done
  1574. Revision 1.91 2003/05/23 15:03:31 peter
  1575. * fix previous commit for unix
  1576. Revision 1.90 2003/05/23 14:39:56 peter
  1577. * FindFilePChar added to allow PATH variables > 256 chars
  1578. Revision 1.89 2003/05/15 18:58:53 peter
  1579. * removed selfpointer_offset, vmtpointer_offset
  1580. * tvarsym.adjusted_address
  1581. * address in localsymtable is now in the real direction
  1582. * removed some obsolete globals
  1583. Revision 1.88 2003/04/27 11:21:32 peter
  1584. * aktprocdef renamed to current_procdef
  1585. * procinfo renamed to current_procinfo
  1586. * procinfo will now be stored in current_module so it can be
  1587. cleaned up properly
  1588. * gen_main_procsym changed to create_main_proc and release_main_proc
  1589. to also generate a tprocinfo structure
  1590. * fixed unit implicit initfinal
  1591. Revision 1.87 2003/04/27 07:29:50 peter
  1592. * current_procdef cleanup, current_procdef is now always nil when parsing
  1593. a new procdef declaration
  1594. * aktprocsym removed
  1595. * lexlevel removed, use symtable.symtablelevel instead
  1596. * implicit init/final code uses the normal genentry/genexit
  1597. * funcret state checking updated for new funcret handling
  1598. Revision 1.86 2003/04/25 20:59:33 peter
  1599. * removed funcretn,funcretsym, function result is now in varsym
  1600. and aliases for result and function name are added using absolutesym
  1601. * vs_hidden parameter for funcret passed in parameter
  1602. * vs_hidden fixes
  1603. * writenode changed to printnode and released from extdebug
  1604. * -vp option added to generate a tree.log with the nodetree
  1605. * nicer printnode for statements, callnode
  1606. Revision 1.85 2003/04/22 14:33:38 peter
  1607. * removed some notes/hints
  1608. Revision 1.84 2003/03/23 23:21:42 hajny
  1609. + emx target added
  1610. Revision 1.83 2003/01/30 21:45:53 peter
  1611. * amiga path fix (merged)
  1612. Revision 1.82 2003/01/12 15:42:23 peter
  1613. * m68k pathexist update from 1.0.x
  1614. * palmos res update from 1.0.x
  1615. Revision 1.81 2003/01/10 21:49:00 marco
  1616. * more hasunix fixes
  1617. Revision 1.80 2003/01/04 16:20:44 hajny
  1618. * modified to make use of the common GetEnv code under OS/2
  1619. Revision 1.79 2002/12/25 01:26:17 peter
  1620. * delphi also has pointer archimetic
  1621. Revision 1.78 2002/12/07 14:27:07 carl
  1622. * 3% memory optimization
  1623. * changed some types
  1624. + added type checking with different size for call node and for
  1625. parameters
  1626. Revision 1.77 2002/12/06 17:50:00 peter
  1627. * amiga fixes merged
  1628. Revision 1.76 2002/12/01 22:07:41 carl
  1629. * warning of portabilitiy problems with parasize / localsize
  1630. + some added documentation
  1631. Revision 1.75 2002/11/30 23:13:48 carl
  1632. - cs_fp_emulation is no longer automatic for m68k target
  1633. Revision 1.74 2002/11/30 11:08:46 carl
  1634. * fix bug n last commit (this bug was only detected gby 1.0.x, fixed)
  1635. Revision 1.73 2002/11/30 00:34:20 pierre
  1636. * remove double in delphimodeswitches to allow compilation with -Cr option
  1637. Revision 1.72 2002/11/29 22:31:19 carl
  1638. + unimplemented hint directive added
  1639. * hint directive parsing implemented
  1640. * warning on these directives
  1641. Revision 1.71 2002/11/20 10:11:46 mazen
  1642. + TSearchPathList.AddPath supports a local path in addition to file name
  1643. Revision 1.70 2002/11/16 14:49:12 carl
  1644. - browser information is off by default
  1645. Revision 1.69 2002/11/15 01:58:47 peter
  1646. * merged changes from 1.0.7 up to 04-11
  1647. - -V option for generating bug report tracing
  1648. - more tracing for option parsing
  1649. - errors for cdecl and high()
  1650. - win32 import stabs
  1651. - win32 records<=8 are returned in eax:edx (turned off by default)
  1652. - heaptrc update
  1653. - more info for temp management in .s file with EXTDEBUG
  1654. Revision 1.68 2002/11/09 15:38:39 carl
  1655. + added var_align/const_align routines
  1656. Revision 1.67 2002/10/16 19:01:43 peter
  1657. + $IMPLICITEXCEPTIONS switch to turn on/off generation of the
  1658. implicit exception frames for procedures with initialized variables
  1659. and for constructors. The default is on for compatibility
  1660. Revision 1.66 2002/09/05 19:28:29 peter
  1661. * removed repetitive pass counting
  1662. * display heapsize also for extdebug
  1663. Revision 1.65 2002/08/19 19:36:42 peter
  1664. * More fixes for cross unit inlining, all tnodes are now implemented
  1665. * Moved pocall_internconst to po_internconst because it is not a
  1666. calling type at all and it conflicted when inlining of these small
  1667. functions was requested
  1668. Revision 1.64 2002/08/12 15:08:39 carl
  1669. + stab register indexes for powerpc (moved from gdb to cpubase)
  1670. + tprocessor enumeration moved to cpuinfo
  1671. + linker in target_info is now a class
  1672. * many many updates for m68k (will soon start to compile)
  1673. - removed some ifdef or correct them for correct cpu
  1674. Revision 1.63 2002/08/10 14:46:29 carl
  1675. + moved target_cpu_string to cpuinfo
  1676. * renamed asmmode enum.
  1677. * assembler reader has now less ifdef's
  1678. * move from nppcmem.pas -> ncgmem.pas vec. node.
  1679. Revision 1.62 2002/07/28 20:45:22 florian
  1680. + added direct assembler reader for PowerPC
  1681. Revision 1.61 2002/07/20 17:12:42 florian
  1682. + source code page support
  1683. Revision 1.60 2002/07/01 18:46:22 peter
  1684. * internal linker
  1685. * reorganized aasm layer
  1686. Revision 1.59 2002/07/01 16:23:52 peter
  1687. * cg64 patch
  1688. * basics for currency
  1689. * asnode updates for class and interface (not finished)
  1690. Revision 1.58 2002/05/18 13:34:08 peter
  1691. * readded missing revisions
  1692. Revision 1.57 2002/05/16 19:46:36 carl
  1693. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1694. + try to fix temp allocation (still in ifdef)
  1695. + generic constructor calls
  1696. + start of tassembler / tmodulebase class cleanup
  1697. Revision 1.55 2002/04/20 21:32:23 carl
  1698. + generic FPC_CHECKPOINTER
  1699. + first parameter offset in stack now portable
  1700. * rename some constants
  1701. + move some cpu stuff to other units
  1702. - remove unused constents
  1703. * fix stacksize for some targets
  1704. * fix generic size problems which depend now on EXTEND_SIZE constant
  1705. Revision 1.54 2002/04/07 13:24:30 carl
  1706. + moved constant from cpuinfo, these are NOT cpu specific
  1707. constant, but more related to compiler functionality.
  1708. Revision 1.53 2002/04/02 17:11:28 peter
  1709. * tlocation,treference update
  1710. * LOC_CONSTANT added for better constant handling
  1711. * secondadd splitted in multiple routines
  1712. * location_force_reg added for loading a location to a register
  1713. of a specified size
  1714. * secondassignment parses now first the right and then the left node
  1715. (this is compatible with Kylix). This saves a lot of push/pop especially
  1716. with string operations
  1717. * adapted some routines to use the new cg methods
  1718. Revision 1.52 2002/03/28 16:07:52 armin
  1719. + initialize threadvars defined local in units
  1720. Revision 1.51 2002/01/24 18:25:48 peter
  1721. * implicit result variable generation for assembler routines
  1722. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  1723. }