globals.pas 55 KB

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