2
0

globals.pas 54 KB

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