globals.pas 56 KB

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