globals.pas 55 KB

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