globals.pas 57 KB

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