globals.pas 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements some support functions and global variables
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit globals;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. {$ifdef windows}
  22. windows,
  23. {$endif}
  24. {$ifdef os2}
  25. dos,
  26. {$endif os2}
  27. {$ifdef hasunix}
  28. Baseunix,unix,
  29. {$endif}
  30. {$IFNDEF USE_FAKE_SYSUTILS}
  31. sysutils,
  32. {$ELSE}
  33. fksysutl,
  34. {$ENDIF}
  35. { comphook pulls in sysutils anyways }
  36. cutils,cclasses,cfileutl,
  37. cpuinfo,
  38. globtype,version,systems;
  39. const
  40. delphimodeswitches =
  41. [m_delphi,m_all,m_class,m_objpas,m_result,m_string_pchar,
  42. m_pointer_2_procedure,m_autoderef,m_tp_procvar,m_initfinal,m_default_ansistring,
  43. m_out,m_default_para,m_duplicate_names,m_hintdirective,
  44. m_property,m_default_inline,m_except,m_advanced_records];
  45. fpcmodeswitches =
  46. [m_fpc,m_all,m_string_pchar,m_nested_comment,m_repeat_forward,
  47. m_cvar_support,m_initfinal,m_hintdirective,
  48. m_property,m_default_inline];
  49. objfpcmodeswitches =
  50. [m_objfpc,m_fpc,m_all,m_class,m_objpas,m_result,m_string_pchar,m_nested_comment,
  51. m_repeat_forward,m_cvar_support,m_initfinal,m_out,m_default_para,m_hintdirective,
  52. m_property,m_default_inline,m_except];
  53. tpmodeswitches =
  54. [m_tp7,m_all,m_tp_procvar,m_duplicate_names];
  55. {$ifdef gpc_mode}
  56. gpcmodeswitches =
  57. [m_gpc,m_all,m_tp_procvar];
  58. {$endif}
  59. macmodeswitches =
  60. [m_mac,m_all,m_cvar_support,m_mac_procvar,m_nested_procvars,m_non_local_goto];
  61. isomodeswitches =
  62. [m_iso,m_all,m_tp_procvar,m_duplicate_names,m_nested_procvars,m_non_local_goto];
  63. { maximum nesting of routines }
  64. maxnesting = 32;
  65. { Filenames and extensions }
  66. sourceext = '.pp';
  67. pasext = '.pas';
  68. pext = '.p';
  69. treelogfilename = 'tree.log';
  70. {$if defined(CPUARM) and defined(FPUFPA)}
  71. MathQNaN : tdoublerec = (bytes : (0,0,252,255,0,0,0,0));
  72. MathInf : tdoublerec = (bytes : (0,0,240,127,0,0,0,0));
  73. MathNegInf : tdoublerec = (bytes : (0,0,240,255,0,0,0,0));
  74. MathPi : tdoublerec = (bytes : (251,33,9,64,24,45,68,84));
  75. {$else}
  76. {$ifdef FPC_LITTLE_ENDIAN}
  77. MathQNaN : tdoublerec = (bytes : (0,0,0,0,0,0,252,255));
  78. MathInf : tdoublerec = (bytes : (0,0,0,0,0,0,240,127));
  79. MathNegInf : tdoublerec = (bytes : (0,0,0,0,0,0,240,255));
  80. MathPi : tdoublerec = (bytes : (24,45,68,84,251,33,9,64));
  81. MathPiExtended : textendedrec = (bytes : (53,194,104,33,162,218,15,201,0,64));
  82. {$else FPC_LITTLE_ENDIAN}
  83. MathQNaN : tdoublerec = (bytes : (255,252,0,0,0,0,0,0));
  84. MathInf : tdoublerec = (bytes : (127,240,0,0,0,0,0,0));
  85. MathNegInf : tdoublerec = (bytes : (255,240,0,0,0,0,0,0));
  86. MathPi : tdoublerec = (bytes : (64,9,33,251,84,68,45,24));
  87. MathPiExtended : textendedrec = (bytes : (64,0,201,15,218,162,33,104,194,53));
  88. {$endif FPC_LITTLE_ENDIAN}
  89. {$endif}
  90. type
  91. tcodepagestring = string[20];
  92. { this is written to ppus during token recording for generics so it must be packed }
  93. tsettings = packed record
  94. alignment : talignmentinfo;
  95. globalswitches : tglobalswitches;
  96. moduleswitches : tmoduleswitches;
  97. localswitches : tlocalswitches;
  98. modeswitches : tmodeswitches;
  99. optimizerswitches : toptimizerswitches;
  100. { generate information necessary to perform these wpo's during a subsequent compilation }
  101. genwpoptimizerswitches: twpoptimizerswitches;
  102. { perform these wpo's using information generated during a previous compilation }
  103. dowpoptimizerswitches: twpoptimizerswitches;
  104. debugswitches : tdebugswitches;
  105. { 0: old behaviour for sets <=256 elements
  106. >0: round to this size }
  107. setalloc,
  108. packenum : shortint;
  109. packrecords : shortint;
  110. maxfpuregisters : shortint;
  111. cputype,
  112. optimizecputype : tcputype;
  113. fputype : tfputype;
  114. asmmode : tasmmode;
  115. interfacetype : tinterfacetypes;
  116. defproccall : tproccalloption;
  117. sourcecodepage : tcodepagestring;
  118. minfpconstprec : tfloattype;
  119. disabledircache : boolean;
  120. { CPU targets with microcontroller support can add a controller specific unit }
  121. {$if defined(ARM) or defined(AVR)}
  122. controllertype : tcontrollertype;
  123. {$endif defined(ARM) or defined(AVR)}
  124. end;
  125. const
  126. LinkMapWeightDefault = 1000;
  127. type
  128. TLinkRec = record
  129. Key : AnsiString;
  130. Value : AnsiString; // key expands to valuelist "value"
  131. Weight: longint;
  132. end;
  133. TLinkStrMap = class
  134. private
  135. itemcnt : longint;
  136. fmap : Array Of TLinkRec;
  137. function Lookup(key:Ansistring):longint;
  138. function getlinkrec(i:longint):TLinkRec;
  139. public
  140. procedure Add(key:ansistring;value:AnsiString='';weight:longint=LinkMapWeightDefault);
  141. procedure addseries(keys:AnsiString;weight:longint=LinkMapWeightDefault);
  142. function AddDep(keyvalue:String):boolean;
  143. function AddWeight(keyvalue:String):boolean;
  144. procedure SetValue(key:AnsiString;Weight:Integer);
  145. procedure SortonWeight;
  146. function Find(key:AnsiString):AnsiString;
  147. procedure Expand(src:TCmdStrList;dest: TLinkStrMap);
  148. procedure UpdateWeights(Weightmap:TLinkStrMap);
  149. constructor Create;
  150. property count : longint read itemcnt;
  151. property items[I:longint]:TLinkRec read getlinkrec; default;
  152. end;
  153. tpendingstate = record
  154. nextverbositystr : shortstring;
  155. nextlocalswitches : tlocalswitches;
  156. nextverbosityfullswitch: longint;
  157. nextcallingstr : shortstring;
  158. verbosityfullswitched,
  159. localswitcheschanged : boolean;
  160. end;
  161. var
  162. { specified inputfile }
  163. inputfilepath : string;
  164. inputfilename : string;
  165. { specified outputfile with -o parameter }
  166. outputfilename : string;
  167. outputprefix : pshortstring;
  168. outputsuffix : pshortstring;
  169. { specified with -FE or -FU }
  170. outputexedir : TPathStr;
  171. outputunitdir : TPathStr;
  172. { specified with -FW and -Fw }
  173. wpofeedbackinput,
  174. wpofeedbackoutput : TPathStr;
  175. { things specified with parameters }
  176. paratarget : tsystem;
  177. paratargetdbg : tdbg;
  178. paratargetasm : tasm;
  179. paralinkoptions : TCmdStr;
  180. paradynamiclinker : string;
  181. paraprintnodetree : byte;
  182. parapreprocess : boolean;
  183. printnodefile : text;
  184. { typical cross compiling params}
  185. { directory where the utils can be found (options -FD) }
  186. utilsdirectory : TPathStr;
  187. { targetname specific prefix used by these utils (options -XP<path>) }
  188. utilsprefix : TCmdStr;
  189. cshared : boolean; { pass --shared to ld to link C libs shared}
  190. Dontlinkstdlibpath: Boolean; { Don't add std paths to linkpath}
  191. rlinkpath : TCmdStr; { rpath-link linkdir override}
  192. sysrootpath : TCmdStr; { target system root to search dyn linker }
  193. { some flags for global compiler switches }
  194. do_build,
  195. do_release,
  196. do_make : boolean;
  197. { path for searching units, different paths can be seperated by ; }
  198. exepath : TPathStr; { Path to ppc }
  199. librarysearchpath,
  200. unitsearchpath,
  201. objectsearchpath,
  202. includesearchpath,
  203. frameworksearchpath : TSearchPathList;
  204. autoloadunits : string;
  205. { linking }
  206. usewindowapi : boolean;
  207. description : string;
  208. SetPEFlagsSetExplicity,
  209. ImageBaseSetExplicity,
  210. MinStackSizeSetExplicity,
  211. MaxStackSizeSetExplicity,
  212. DescriptionSetExplicity : boolean;
  213. dllversion : string;
  214. dllmajor,
  215. dllminor,
  216. dllrevision : word; { revision only for netware }
  217. { win pe }
  218. peflags : longint;
  219. minstacksize,
  220. maxstacksize,
  221. imagebase : puint;
  222. UseDeffileForExports : boolean;
  223. UseDeffileForExportsSetExplicitly : boolean;
  224. GenerateImportSection,
  225. GenerateImportSectionSetExplicitly,
  226. RelocSection : boolean;
  227. RelocSectionSetExplicitly : boolean;
  228. LinkTypeSetExplicitly : boolean;
  229. current_tokenpos, { position of the last token }
  230. current_filepos : tfileposinfo; { current position }
  231. nwscreenname : string;
  232. nwthreadname : string;
  233. nwcopyright : string;
  234. codegenerror : boolean; { true if there is an error reported }
  235. block_type : tblock_type; { type of currently parsed block }
  236. compile_level : word;
  237. exceptblockcounter : integer; { each except block gets a unique number check gotos }
  238. current_exceptblock : integer; { the exceptblock number of the current block (0 if none) }
  239. LinkLibraryAliases : TLinkStrMap;
  240. LinkLibraryOrder : TLinkStrMap;
  241. init_settings,
  242. current_settings : tsettings;
  243. pendingstate : tpendingstate;
  244. { Memory sizes }
  245. heapsize,
  246. stacksize,
  247. jmp_buf_size,
  248. jmp_buf_align : longint;
  249. {$Ifdef EXTDEBUG}
  250. { parameter switches }
  251. debugstop : boolean;
  252. {$EndIf EXTDEBUG}
  253. { windows / OS/2 application type }
  254. apptype : tapptype;
  255. features : tfeatures;
  256. const
  257. DLLsource : boolean = false;
  258. { used to set all registers used for each global function
  259. this should dramatically decrease the number of
  260. recompilations needed PM }
  261. simplify_ppu : boolean = true;
  262. Inside_asm_statement : boolean = false;
  263. global_unit_count : word = 0;
  264. { for error info in pp.pas }
  265. parser_current_file : string = '';
  266. {$if defined(m68k) or defined(arm)}
  267. { PalmOS resources }
  268. palmos_applicationname : string = 'FPC Application';
  269. palmos_applicationid : string[4] = 'FPCA';
  270. {$endif defined(m68k) or defined(arm)}
  271. {$ifdef powerpc}
  272. { default calling convention used on MorphOS }
  273. syscall_convention : string = 'LEGACY';
  274. {$endif powerpc}
  275. { default name of the C-style "main" procedure of the library/program }
  276. { (this will be prefixed with the target_info.cprefix) }
  277. defaultmainaliasname = 'main';
  278. mainaliasname : string = defaultmainaliasname;
  279. { by default no local variable trashing }
  280. localvartrashing: longint = -1;
  281. { actual values are defined in ncgutil.pas }
  282. nroftrashvalues = 4;
  283. const
  284. default_settings : TSettings = (
  285. alignment : (
  286. procalign : 0;
  287. loopalign : 0;
  288. jumpalign : 0;
  289. constalignmin : 0;
  290. constalignmax : 0;
  291. varalignmin : 0;
  292. varalignmax : 0;
  293. localalignmin : 0;
  294. localalignmax : 0;
  295. recordalignmin : 0;
  296. recordalignmax : 0;
  297. maxCrecordalign : 0;
  298. );
  299. globalswitches : [cs_check_unit_name,cs_link_static];
  300. moduleswitches : [cs_extsyntax,cs_implicit_exceptions];
  301. localswitches : [cs_check_io,cs_typed_const_writable,cs_pointermath];
  302. modeswitches : fpcmodeswitches;
  303. optimizerswitches : [];
  304. genwpoptimizerswitches : [];
  305. dowpoptimizerswitches : [];
  306. debugswitches : [];
  307. setalloc : 0;
  308. packenum : 4;
  309. packrecords : 0;
  310. maxfpuregisters : 0;
  311. {$ifdef i386}
  312. cputype : cpu_Pentium;
  313. optimizecputype : cpu_Pentium3;
  314. fputype : fpu_x87;
  315. {$endif i386}
  316. {$ifdef m68k}
  317. cputype : cpu_MC68020;
  318. optimizecputype : cpu_MC68020;
  319. fputype : fpu_soft;
  320. {$endif m68k}
  321. {$ifdef powerpc}
  322. cputype : cpu_PPC604;
  323. optimizecputype : cpu_ppc7400;
  324. fputype : fpu_standard;
  325. {$endif powerpc}
  326. {$ifdef POWERPC64}
  327. cputype : cpu_PPC970;
  328. optimizecputype : cpu_ppc970;
  329. fputype : fpu_standard;
  330. {$endif POWERPC64}
  331. {$ifdef sparc}
  332. cputype : cpu_SPARC_V8;
  333. optimizecputype : cpu_SPARC_V8;
  334. fputype : fpu_hard;
  335. {$endif sparc}
  336. {$ifdef arm}
  337. cputype : cpu_armv3;
  338. optimizecputype : cpu_armv3;
  339. fputype : fpu_fpa;
  340. {$endif arm}
  341. {$ifdef x86_64}
  342. cputype : cpu_athlon64;
  343. optimizecputype : cpu_athlon64;
  344. fputype : fpu_sse64;
  345. {$endif x86_64}
  346. {$ifdef avr}
  347. cputype : cpuinfo.cpu_avr;
  348. optimizecputype : cpuinfo.cpu_avr;
  349. fputype : fpu_none;
  350. {$endif avr}
  351. {$ifdef mips}
  352. cputype : cpu_mips32;
  353. optimizecputype : cpu_mips32;
  354. fputype : fpu_mips2;
  355. {$endif mips}
  356. asmmode : asmmode_standard;
  357. interfacetype : it_interfacecom;
  358. defproccall : pocall_default;
  359. sourcecodepage : '8859-1';
  360. minfpconstprec : s32real;
  361. disabledircache : false;
  362. {$if defined(ARM)}
  363. controllertype : ct_none;
  364. {$endif defined(ARM)}
  365. );
  366. var
  367. starttime : real;
  368. function getdatestr:string;
  369. function gettimestr:string;
  370. function filetimestring( t : longint) : string;
  371. function getrealtime : real;
  372. procedure DefaultReplacements(var s:ansistring);
  373. function Shell(const command:ansistring): longint;
  374. function GetEnvPChar(const envname:string):pchar;
  375. procedure FreeEnvPChar(p:pchar);
  376. function is_number_float(d : double) : boolean;
  377. { discern +0.0 and -0.0 }
  378. function get_real_sign(r: bestreal): longint;
  379. procedure InitGlobals;
  380. procedure DoneGlobals;
  381. function string2guid(const s: string; var GUID: TGUID): boolean;
  382. function guid2string(const GUID: TGUID): string;
  383. function SetAktProcCall(const s:string; var a:tproccalloption):boolean;
  384. function Setabitype(const s:string;var a:tabi):boolean;
  385. function Setcputype(const s:string;var a:tcputype):boolean;
  386. function SetFpuType(const s:string;var a:tfputype):boolean;
  387. {$if defined(arm) or defined(avr)}
  388. function SetControllerType(const s:string;var a:tcontrollertype):boolean;
  389. {$endif defined(arm) or defined(avr)}
  390. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  391. function UpdateOptimizerStr(s:string;var a:toptimizerswitches):boolean;
  392. function UpdateWpoStr(s: string; var a: twpoptimizerswitches): boolean;
  393. function UpdateDebugStr(s:string;var a:tdebugswitches):boolean;
  394. function IncludeFeature(const s : string) : boolean;
  395. function SetMinFPConstPrec(const s: string; var a: tfloattype) : boolean;
  396. {# Routine to get the required alignment for size of data, which will
  397. be placed in bss segment, according to the current alignment requirements }
  398. function var_align(want_align: longint): shortint;
  399. function var_align_size(siz: longint): shortint;
  400. {# Routine to get the required alignment for size of data, which will
  401. be placed in data/const segment, according to the current alignment requirements }
  402. function const_align(want_align: longint): shortint;
  403. function const_align_size(siz: longint): shortint;
  404. {$ifdef ARM}
  405. function is_double_hilo_swapped: boolean;{$ifdef USEINLINE}inline;{$endif}
  406. {$endif ARM}
  407. function floating_point_range_check_error : boolean;
  408. implementation
  409. uses
  410. {$ifdef macos}
  411. macutils,
  412. {$endif}
  413. comphook;
  414. {****************************************************************************
  415. TLinkStrMap
  416. ****************************************************************************}
  417. Constructor TLinkStrMap.create;
  418. begin
  419. inherited;
  420. itemcnt:=0;
  421. end;
  422. procedure TLinkStrMap.Add(key:ansistring;value:AnsiString='';weight:longint=LinkMapWeightDefault);
  423. begin
  424. if lookup(key)<>-1 Then
  425. exit;
  426. if itemcnt<=length(fmap) Then
  427. setlength(fmap,itemcnt+10);
  428. fmap[itemcnt].key:=key;
  429. fmap[itemcnt].value:=value;
  430. fmap[itemcnt].weight:=weight;
  431. inc(itemcnt);
  432. end;
  433. function TLinkStrMap.AddDep(keyvalue:String):boolean;
  434. var
  435. i : Longint;
  436. begin
  437. AddDep:=false;
  438. i:=pos('=',keyvalue);
  439. if i=0 then
  440. exit;
  441. Add(Copy(KeyValue,1,i-1),Copy(KeyValue,i+1,length(KeyValue)-i));
  442. AddDep:=True;
  443. end;
  444. function TLinkStrMap.AddWeight(keyvalue:String):boolean;
  445. var
  446. i,j : Longint;
  447. Code : Word;
  448. s : AnsiString;
  449. begin
  450. AddWeight:=false;
  451. i:=pos('=',keyvalue);
  452. if i=0 then
  453. exit;
  454. s:=Copy(KeyValue,i+1,length(KeyValue)-i);
  455. val(s,j,code);
  456. if code=0 Then
  457. begin
  458. Add(Copy(KeyValue,1,i-1),'',j);
  459. AddWeight:=True;
  460. end;
  461. end;
  462. procedure TLinkStrMap.addseries(keys:AnsiString;weight:longint);
  463. var
  464. i,j,k : longint;
  465. begin
  466. k:=length(keys);
  467. i:=1;
  468. while i<=k do
  469. begin
  470. j:=i;
  471. while (i<=k) and (keys[i]<>',') do
  472. inc(i);
  473. add(copy(keys,j,i-j),'',weight);
  474. inc(i);
  475. end;
  476. end;
  477. procedure TLinkStrMap.SetValue(Key:Ansistring;weight:Integer);
  478. var
  479. j : longint;
  480. begin
  481. j:=lookup(key);
  482. if j<>-1 then
  483. fmap[j].weight:=weight;
  484. end;
  485. function TLinkStrMap.find(key:Ansistring):Ansistring;
  486. var
  487. j : longint;
  488. begin
  489. find:='';
  490. j:=lookup(key);
  491. if j<>-1 then
  492. find:=fmap[j].value;
  493. end;
  494. function TLinkStrMap.lookup(key:Ansistring):longint;
  495. var
  496. i : longint;
  497. begin
  498. lookup:=-1;
  499. i:=0;
  500. while (i<itemcnt) and (fmap[i].key<>key) do
  501. inc(i);
  502. if i<>itemcnt then
  503. lookup:=i;
  504. end;
  505. procedure TLinkStrMap.SortOnWeight;
  506. var
  507. i, j : longint;
  508. m : TLinkRec;
  509. begin
  510. if itemcnt <2 then exit;
  511. for i:=0 to itemcnt-1 do
  512. for j:=i+1 to itemcnt-1 do
  513. begin
  514. if fmap[i].weight>fmap[j].weight Then
  515. begin
  516. m:=fmap[i];
  517. fmap[i]:=fmap[j];
  518. fmap[j]:=m;
  519. end;
  520. end;
  521. end;
  522. function TLinkStrMap.getlinkrec(i:longint):TLinkRec;
  523. begin
  524. result:=fmap[i];
  525. end;
  526. procedure TLinkStrMap.Expand(Src:TCmdStrList;Dest:TLinkStrMap);
  527. // expands every thing in Src to Dest for linkorder purposes.
  528. var
  529. r : longint;
  530. LibN : TCmdStr;
  531. begin
  532. while not src.empty do
  533. begin
  534. LibN:=src.getfirst;
  535. r:=lookup (LibN);
  536. if r=-1 then
  537. dest.add(LibN)
  538. else
  539. dest.addseries(fmap[r].value);
  540. end;
  541. end;
  542. procedure TLinkStrMap.UpdateWeights(Weightmap:TLinkStrMap);
  543. var
  544. l,r : longint;
  545. begin
  546. for l := 0 to itemcnt-1 do
  547. begin
  548. r:=weightmap.lookup (fmap[l].key);
  549. if r<>-1 then
  550. fmap[l].weight:=weightmap[r].weight;
  551. end;
  552. end;
  553. {****************************************************************************
  554. Time Handling
  555. ****************************************************************************}
  556. Function L0(l:longint):string;
  557. {
  558. return the string of value l, if l<10 then insert a zero, so
  559. the string is always at least 2 chars '01','02',etc
  560. }
  561. var
  562. s : string;
  563. begin
  564. Str(l,s);
  565. if l<10 then
  566. s:='0'+s;
  567. L0:=s;
  568. end;
  569. function gettimestr:string;
  570. {
  571. get the current time in a string HH:MM:SS
  572. }
  573. var
  574. hour,min,sec,hsec : word;
  575. begin
  576. DecodeTime(Time,hour,min,sec,hsec);
  577. gettimestr:=L0(Hour)+':'+L0(min)+':'+L0(sec);
  578. end;
  579. function getdatestr:string;
  580. {
  581. get the current date in a string YY/MM/DD
  582. }
  583. var
  584. Year,Month,Day: Word;
  585. begin
  586. DecodeDate(Date,year,month,day);
  587. getdatestr:=L0(Year)+'/'+L0(Month)+'/'+L0(Day);
  588. end;
  589. function filetimestring( t : longint) : string;
  590. {
  591. convert dos datetime t to a string YY/MM/DD HH:MM:SS
  592. }
  593. var
  594. DT : TDateTime;
  595. hsec : word;
  596. Year,Month,Day: Word;
  597. hour,min,sec : word;
  598. begin
  599. if t=-1 then
  600. begin
  601. Result := 'Not Found';
  602. exit;
  603. end;
  604. DT := FileDateToDateTime(t);
  605. DecodeTime(DT,hour,min,sec,hsec);
  606. DecodeDate(DT,year,month,day);
  607. Result := L0(Year)+'/'+L0(Month)+'/'+L0(Day)+' '+L0(Hour)+':'+L0(min)+':'+L0(sec);
  608. end;
  609. function getrealtime : real;
  610. var
  611. h,m,s,s1000 : word;
  612. begin
  613. DecodeTime(Time,h,m,s,s1000);
  614. result:=h*3600.0+m*60.0+s+s1000/1000.0;
  615. end;
  616. {****************************************************************************
  617. Default Macro Handling
  618. ****************************************************************************}
  619. {$ifdef windows}
  620. {
  621. This code is copied from sysutils.pp
  622. }
  623. Type
  624. PFNSHGetFolderPath = Function(Ahwnd: HWND; Csidl: Integer; Token: THandle; Flags: DWord; Path: PChar): HRESULT; stdcall;
  625. var
  626. SHGetFolderPath : PFNSHGetFolderPath = Nil;
  627. CFGDLLHandle : THandle = 0;
  628. const
  629. CSIDL_PERSONAL = $0005; { %USERPROFILE%\My Documents }
  630. CSIDL_APPDATA = $001A; { %USERPROFILE%\Application Data (roaming) }
  631. CSIDL_LOCAL_APPDATA = $001C; { %USERPROFILE%\Local Settings\Application Data (non roaming) }
  632. CSIDL_COMMON_APPDATA = $0023; { %PROFILESPATH%\All Users\Application Data }
  633. CSIDL_PROGRAM_FILES = $0026; { %SYSTEMDRIVE%\Program Files }
  634. CSIDL_PROFILE = $0028; { %USERPROFILE% }
  635. CSIDL_PROGRAM_FILES_COMMON = $002B; { %SYSTEMDRIVE%\Program Files\Common }
  636. CSIDL_FLAG_CREATE = $8000; { (force creation of requested folder if it doesn't exist yet) }
  637. Procedure InitDLL;
  638. Var
  639. pathBuf: array[0..MAX_PATH-1] of char;
  640. pathLength: Integer;
  641. begin
  642. { Load shfolder.dll using a full path, in order to prevent spoofing (Mantis #18185)
  643. Don't bother loading shell32.dll because shfolder.dll itself redirects SHGetFolderPath
  644. to shell32.dll whenever possible. }
  645. pathLength:=GetSystemDirectory(pathBuf, MAX_PATH);
  646. if (pathLength>0) and (pathLength<MAX_PATH-14) then { 14=length('\shfolder.dll'#0) }
  647. begin
  648. StrLCopy(@pathBuf[pathLength],'\shfolder.dll',MAX_PATH-pathLength-1);
  649. CFGDLLHandle:=LoadLibrary(pathBuf);
  650. if (CFGDLLHandle<>0) then
  651. begin
  652. Pointer(ShGetFolderPath):=GetProcAddress(CFGDLLHandle,'SHGetFolderPathA');
  653. If @ShGetFolderPath=nil then
  654. begin
  655. FreeLibrary(CFGDLLHandle);
  656. CFGDllHandle:=0;
  657. end;
  658. end;
  659. end;
  660. If (@ShGetFolderPath=Nil) then
  661. Raise Exception.Create('Could not determine SHGetFolderPath Function');
  662. end;
  663. Function GetSpecialDir(ID : Integer) : String;
  664. Var
  665. APath : Array[0..MAX_PATH] of char;
  666. begin
  667. Result:='';
  668. if (CFGDLLHandle=0) then
  669. InitDLL;
  670. If (SHGetFolderPath<>Nil) then
  671. begin
  672. if SHGetFolderPath(0,ID or CSIDL_FLAG_CREATE,0,0,@APATH[0])=S_OK then
  673. Result:=IncludeTrailingPathDelimiter(StrPas(@APath[0]));
  674. end;
  675. end;
  676. {$endif windows}
  677. procedure DefaultReplacements(var s:ansistring);
  678. {$ifdef windows}
  679. procedure ReplaceSpecialFolder(const MacroName: string; const ID: integer);
  680. begin
  681. // Only try to receive the special folders (and thus dynamically
  682. // load shfolder.dll) when that's needed.
  683. if pos(MacroName,s)>0 then
  684. Replace(s,MacroName,GetSpecialDir(ID));
  685. end;
  686. {$endif windows}
  687. var
  688. envstr: string;
  689. envvalue: pchar;
  690. i: integer;
  691. begin
  692. { Replace some macros }
  693. Replace(s,'$FPCVERSION',version_string);
  694. Replace(s,'$FPCFULLVERSION',full_version_string);
  695. Replace(s,'$FPCDATE',date_string);
  696. Replace(s,'$FPCCPU',target_cpu_string);
  697. Replace(s,'$FPCOS',target_os_string);
  698. if tf_use_8_3 in Source_Info.Flags then
  699. Replace(s,'$FPCTARGET',target_os_string)
  700. else
  701. Replace(s,'$FPCTARGET',target_full_string);
  702. {$ifdef windows}
  703. ReplaceSpecialFolder('$LOCAL_APPDATA',CSIDL_LOCAL_APPDATA);
  704. ReplaceSpecialFolder('$APPDATA',CSIDL_APPDATA);
  705. ReplaceSpecialFolder('$COMMON_APPDATA',CSIDL_COMMON_APPDATA);
  706. ReplaceSpecialFolder('$PERSONAL',CSIDL_PERSONAL);
  707. ReplaceSpecialFolder('$PROGRAM_FILES',CSIDL_PROGRAM_FILES);
  708. ReplaceSpecialFolder('$PROGRAM_FILES_COMMON',CSIDL_PROGRAM_FILES_COMMON);
  709. ReplaceSpecialFolder('$PROFILE',CSIDL_PROFILE);
  710. {$endif windows}
  711. { Replace environment variables between dollar signs }
  712. i := pos('$',s);
  713. while i>0 do
  714. begin
  715. envstr:=copy(s,i+1,length(s)-i);
  716. i:=pos('$',envstr);
  717. if i>0 then
  718. begin
  719. envstr := copy(envstr,1,i-1);
  720. envvalue := GetEnvPChar(envstr);
  721. if assigned(envvalue) then
  722. begin
  723. Replace(s,'$'+envstr+'$',envvalue);
  724. // Look if there is another env.var in the string
  725. i:=pos('$',s);
  726. end
  727. else
  728. // if the env.var is not set, do not replace the env.variable
  729. // and stop looking for more env.var within the string
  730. i := 0;
  731. FreeEnvPChar(envvalue);
  732. end;
  733. end;
  734. end;
  735. {****************************************************************************
  736. OS Dependent things
  737. ****************************************************************************}
  738. function GetEnvPChar(const envname:string):pchar;
  739. {$ifdef win32}
  740. var
  741. s : string;
  742. i,len : longint;
  743. hp,p,p2 : pchar;
  744. {$endif}
  745. begin
  746. {$ifdef hasunix}
  747. GetEnvPchar:=BaseUnix.fpGetEnv(envname);
  748. {$define GETENVOK}
  749. {$endif}
  750. {$ifdef win32}
  751. GetEnvPchar:=nil;
  752. p:=GetEnvironmentStrings;
  753. hp:=p;
  754. while hp^<>#0 do
  755. begin
  756. s:=strpas(hp);
  757. i:=pos('=',s);
  758. len:=strlen(hp);
  759. if upper(copy(s,1,i-1))=upper(envname) then
  760. begin
  761. GetMem(p2,len-length(envname));
  762. Move(hp[i],p2^,len-length(envname));
  763. GetEnvPchar:=p2;
  764. break;
  765. end;
  766. { next string entry}
  767. hp:=hp+len+1;
  768. end;
  769. FreeEnvironmentStrings(p);
  770. {$define GETENVOK}
  771. {$endif}
  772. {$ifdef os2}
  773. GetEnvPChar := Dos.GetEnvPChar (EnvName);
  774. {$define GETENVOK}
  775. {$endif}
  776. {$ifdef GETENVOK}
  777. {$undef GETENVOK}
  778. {$else}
  779. GetEnvPchar:=StrPNew(GetEnvironmentVariable(envname));
  780. {$endif}
  781. end;
  782. procedure FreeEnvPChar(p:pchar);
  783. begin
  784. {$ifndef hasunix}
  785. {$ifndef os2}
  786. freemem(p);
  787. {$endif}
  788. {$endif}
  789. end;
  790. {$if defined(MORPHOS) or defined(AMIGA)}
  791. {$define AMIGASHELL}
  792. {$endif}
  793. function Shell(const command:ansistring): longint;
  794. { This is already defined in the linux.ppu for linux, need for the *
  795. expansion under linux }
  796. {$ifdef hasunix}
  797. begin
  798. result := Unix.fpsystem(command);
  799. end;
  800. {$else hasunix}
  801. {$ifdef amigashell}
  802. begin
  803. result := ExecuteProcess('',command);
  804. end;
  805. {$else amigashell}
  806. var
  807. comspec : string;
  808. begin
  809. comspec:=GetEnvironmentVariable('COMSPEC');
  810. result := ExecuteProcess(comspec,' /C '+command);
  811. end;
  812. {$endif amigashell}
  813. {$endif hasunix}
  814. {$UNDEF AMIGASHELL}
  815. function is_number_float(d : double) : boolean;
  816. var
  817. bytearray : array[0..7] of byte;
  818. begin
  819. move(d,bytearray,8);
  820. { only 1.1 save, 1.0.x will use always little endian }
  821. {$ifdef FPC_BIG_ENDIAN}
  822. result:=((bytearray[0] and $7f)<>$7f) or ((bytearray[1] and $f0)<>$f0);
  823. {$else FPC_BIG_ENDIAN}
  824. result:=((bytearray[7] and $7f)<>$7f) or ((bytearray[6] and $f0)<>$f0);
  825. {$endif FPC_BIG_ENDIAN}
  826. end;
  827. function get_real_sign(r: bestreal): longint;
  828. var
  829. p: pbyte;
  830. begin
  831. p := pbyte(@r);
  832. {$ifdef CPU_ARM}
  833. inc(p,4);
  834. {$else}
  835. {$ifdef FPC_LITTLE_ENDIAN}
  836. inc(p,sizeof(r)-1);
  837. {$endif}
  838. {$endif}
  839. if (p^ and $80) = 0 then
  840. result := 1
  841. else
  842. result := -1;
  843. end;
  844. function convertdoublerec(d : tdoublerec) : tdoublerec;{$ifdef USEINLINE}inline;{$endif}
  845. {$ifdef CPUARM}
  846. var
  847. i : longint;
  848. begin
  849. for i:=0 to 3 do
  850. begin
  851. result.bytes[i+4]:=d.bytes[i];
  852. result.bytes[i]:=d.bytes[i+4];
  853. end;
  854. {$else CPUARM}
  855. begin
  856. result:=d;
  857. {$endif CPUARM}
  858. end;
  859. { '('D1:'00000000-'D2:'0000-'D3:'0000-'D4:'0000-000000000000)' }
  860. function string2guid(const s: string; var GUID: TGUID): boolean;
  861. function ishexstr(const hs: string): boolean;
  862. var
  863. i: integer;
  864. begin
  865. ishexstr:=false;
  866. for i:=1 to Length(hs) do begin
  867. if not (hs[i] in ['0'..'9','A'..'F','a'..'f']) then
  868. exit;
  869. end;
  870. ishexstr:=true;
  871. end;
  872. function hexstr2longint(const hexs: string): longint;
  873. var
  874. i: integer;
  875. rl: longint;
  876. begin
  877. rl:=0;
  878. for i:=1 to length(hexs) do begin
  879. rl:=rl shl 4;
  880. case hexs[i] of
  881. '0'..'9' : inc(rl,ord(hexs[i])-ord('0'));
  882. 'A'..'F' : inc(rl,ord(hexs[i])-ord('A')+10);
  883. 'a'..'f' : inc(rl,ord(hexs[i])-ord('a')+10);
  884. end
  885. end;
  886. hexstr2longint:=rl;
  887. end;
  888. var
  889. i: integer;
  890. begin
  891. if (Length(s)=38) and (s[1]='{') and (s[38]='}') and
  892. (s[10]='-') and (s[15]='-') and (s[20]='-') and (s[25]='-') and
  893. ishexstr(copy(s,2,8)) and ishexstr(copy(s,11,4)) and
  894. ishexstr(copy(s,16,4)) and ishexstr(copy(s,21,4)) and
  895. ishexstr(copy(s,26,12)) then begin
  896. GUID.D1:=dword(hexstr2longint(copy(s,2,8)));
  897. { these values are arealdy in the correct range (4 chars = word) }
  898. GUID.D2:=word(hexstr2longint(copy(s,11,4)));
  899. GUID.D3:=word(hexstr2longint(copy(s,16,4)));
  900. for i:=0 to 1 do
  901. GUID.D4[i]:=byte(hexstr2longint(copy(s,21+i*2,2)));
  902. for i:=2 to 7 do
  903. GUID.D4[i]:=byte(hexstr2longint(copy(s,22+i*2,2)));
  904. string2guid:=true;
  905. end
  906. else if (length(s)=0) then
  907. begin
  908. FillChar(GUID,SizeOf(GUID),0);
  909. string2guid:=true;
  910. end
  911. else
  912. string2guid:=false;
  913. end;
  914. function guid2string(const GUID: TGUID): string;
  915. begin
  916. guid2string:=
  917. '{'+hexstr(GUID.D1,8)+
  918. '-'+hexstr(GUID.D2,4)+
  919. '-'+hexstr(GUID.D3,4)+
  920. '-'+hexstr(GUID.D4[0],2)+hexstr(GUID.D4[1],2)+
  921. '-'+hexstr(GUID.D4[2],2)+hexstr(GUID.D4[3],2)+
  922. hexstr(GUID.D4[4],2)+hexstr(GUID.D4[5],2)+
  923. hexstr(GUID.D4[6],2)+hexstr(GUID.D4[7],2)+
  924. '}';
  925. end;
  926. function SetAktProcCall(const s:string; var a:tproccalloption):boolean;
  927. const
  928. DefProcCallName : array[tproccalloption] of string[12] = ('',
  929. 'CDECL',
  930. 'CPPDECL',
  931. 'FAR16',
  932. 'OLDFPCCALL',
  933. '', { internproc }
  934. '', { syscall }
  935. 'PASCAL',
  936. 'REGISTER',
  937. 'SAFECALL',
  938. 'STDCALL',
  939. 'SOFTFLOAT',
  940. 'MWPASCAL'
  941. );
  942. var
  943. t : tproccalloption;
  944. hs : string;
  945. begin
  946. result:=false;
  947. if (s = '') then
  948. exit;
  949. hs:=upper(s);
  950. if (hs = 'DEFAULT') then
  951. begin
  952. a := pocall_default;
  953. result := true;
  954. exit;
  955. end;
  956. for t:=low(tproccalloption) to high(tproccalloption) do
  957. if DefProcCallName[t]=hs then
  958. begin
  959. a:=t;
  960. result:=true;
  961. break;
  962. end;
  963. end;
  964. function Setabitype(const s:string;var a:tabi):boolean;
  965. var
  966. t : tabi;
  967. hs : string;
  968. begin
  969. result:=false;
  970. hs:=Upper(s);
  971. for t:=low(t) to high(t) do
  972. if abi2str[t]=hs then
  973. begin
  974. a:=t;
  975. result:=true;
  976. break;
  977. end;
  978. end;
  979. function Setcputype(const s:string;var a:tcputype):boolean;
  980. var
  981. t : tcputype;
  982. hs : string;
  983. begin
  984. result:=false;
  985. hs:=Upper(s);
  986. for t:=low(tcputype) to high(tcputype) do
  987. if cputypestr[t]=hs then
  988. begin
  989. a:=t;
  990. result:=true;
  991. break;
  992. end;
  993. end;
  994. function SetFpuType(const s:string;var a:tfputype):boolean;
  995. var
  996. t : tfputype;
  997. begin
  998. result:=false;
  999. for t:=low(tfputype) to high(tfputype) do
  1000. if fputypestr[t]=s then
  1001. begin
  1002. a:=t;
  1003. result:=true;
  1004. break;
  1005. end;
  1006. end;
  1007. {$if defined(arm) or defined(avr)}
  1008. function SetControllerType(const s:string;var a:tcontrollertype):boolean;
  1009. var
  1010. t : tcontrollertype;
  1011. hs : string;
  1012. begin
  1013. result:=false;
  1014. hs:=Upper(s);
  1015. for t:=low(tcontrollertype) to high(tcontrollertype) do
  1016. if controllertypestr[t]=hs then
  1017. begin
  1018. a:=t;
  1019. result:=true;
  1020. break;
  1021. end;
  1022. end;
  1023. {$endif defined(arm) or defined(avr)}
  1024. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  1025. var
  1026. tok : string;
  1027. vstr : string;
  1028. l : longint;
  1029. code : integer;
  1030. b : talignmentinfo;
  1031. begin
  1032. UpdateAlignmentStr:=true;
  1033. uppervar(s);
  1034. fillchar(b,sizeof(b),0);
  1035. repeat
  1036. tok:=GetToken(s,'=');
  1037. if tok='' then
  1038. break;
  1039. vstr:=GetToken(s,',');
  1040. val(vstr,l,code);
  1041. if tok='PROC' then
  1042. b.procalign:=l
  1043. else if tok='JUMP' then
  1044. b.jumpalign:=l
  1045. else if tok='LOOP' then
  1046. b.loopalign:=l
  1047. else if tok='CONSTMIN' then
  1048. begin
  1049. b.constalignmin:=l;
  1050. if l>b.constalignmax then
  1051. b.constalignmax:=l;
  1052. end
  1053. else if tok='CONSTMAX' then
  1054. b.constalignmax:=l
  1055. else if tok='VARMIN' then
  1056. begin
  1057. b.varalignmin:=l;
  1058. if l>b.varalignmax then
  1059. b.varalignmax:=l;
  1060. end
  1061. else if tok='VARMAX' then
  1062. b.varalignmax:=l
  1063. else if tok='LOCALMIN' then
  1064. begin
  1065. b.localalignmin:=l;
  1066. if l>b.localalignmax then
  1067. b.localalignmax:=l;
  1068. end
  1069. else if tok='LOCALMAX' then
  1070. b.localalignmax:=l
  1071. else if tok='RECORDMIN' then
  1072. begin
  1073. b.recordalignmin:=l;
  1074. if l>b.recordalignmax then
  1075. b.recordalignmax:=l;
  1076. end
  1077. else if tok='RECORDMAX' then
  1078. b.recordalignmax:=l
  1079. else { Error }
  1080. UpdateAlignmentStr:=false;
  1081. until false;
  1082. UpdateAlignment(a,b);
  1083. end;
  1084. function UpdateOptimizerStr(s:string;var a:toptimizerswitches):boolean;
  1085. var
  1086. tok : string;
  1087. doset,
  1088. found : boolean;
  1089. opt : toptimizerswitch;
  1090. begin
  1091. result:=true;
  1092. uppervar(s);
  1093. repeat
  1094. tok:=GetToken(s,',');
  1095. if tok='' then
  1096. break;
  1097. if Copy(tok,1,2)='NO' then
  1098. begin
  1099. delete(tok,1,2);
  1100. doset:=false;
  1101. end
  1102. else
  1103. doset:=true;
  1104. found:=false;
  1105. for opt:=low(toptimizerswitch) to high(toptimizerswitch) do
  1106. begin
  1107. if OptimizerSwitchStr[opt]=tok then
  1108. begin
  1109. found:=true;
  1110. break;
  1111. end;
  1112. end;
  1113. if found then
  1114. begin
  1115. if doset then
  1116. include(a,opt)
  1117. else
  1118. exclude(a,opt);
  1119. end
  1120. else
  1121. result:=false;
  1122. until false;
  1123. end;
  1124. function UpdateWpoStr(s: string; var a: twpoptimizerswitches): boolean;
  1125. var
  1126. tok : string;
  1127. doset,
  1128. found : boolean;
  1129. opt : twpoptimizerswitch;
  1130. begin
  1131. result:=true;
  1132. uppervar(s);
  1133. repeat
  1134. tok:=GetToken(s,',');
  1135. if tok='' then
  1136. break;
  1137. if Copy(tok,1,2)='NO' then
  1138. begin
  1139. delete(tok,1,2);
  1140. doset:=false;
  1141. end
  1142. else
  1143. doset:=true;
  1144. found:=false;
  1145. if (tok = 'ALL') then
  1146. begin
  1147. for opt:=low(twpoptimizerswitch) to high(twpoptimizerswitch) do
  1148. if doset then
  1149. include(a,opt)
  1150. else
  1151. exclude(a,opt);
  1152. end
  1153. else
  1154. begin
  1155. for opt:=low(twpoptimizerswitch) to high(twpoptimizerswitch) do
  1156. begin
  1157. if WPOptimizerSwitchStr[opt]=tok then
  1158. begin
  1159. found:=true;
  1160. break;
  1161. end;
  1162. end;
  1163. if found then
  1164. begin
  1165. if doset then
  1166. include(a,opt)
  1167. else
  1168. exclude(a,opt);
  1169. end
  1170. else
  1171. result:=false;
  1172. end;
  1173. until false;
  1174. end;
  1175. function UpdateDebugStr(s:string;var a:tdebugswitches):boolean;
  1176. var
  1177. tok : string;
  1178. doset,
  1179. found : boolean;
  1180. opt : tdebugswitch;
  1181. begin
  1182. result:=true;
  1183. uppervar(s);
  1184. repeat
  1185. tok:=GetToken(s,',');
  1186. if tok='' then
  1187. break;
  1188. if Copy(tok,1,2)='NO' then
  1189. begin
  1190. delete(tok,1,2);
  1191. doset:=false;
  1192. end
  1193. else
  1194. doset:=true;
  1195. found:=false;
  1196. for opt:=low(tdebugswitch) to high(tdebugswitch) do
  1197. begin
  1198. if DebugSwitchStr[opt]=tok then
  1199. begin
  1200. found:=true;
  1201. break;
  1202. end;
  1203. end;
  1204. if found then
  1205. begin
  1206. if doset then
  1207. include(a,opt)
  1208. else
  1209. exclude(a,opt);
  1210. end
  1211. else
  1212. result:=false;
  1213. until false;
  1214. end;
  1215. function IncludeFeature(const s : string) : boolean;
  1216. var
  1217. i : tfeature;
  1218. begin
  1219. result:=true;
  1220. for i:=low(tfeature) to high(tfeature) do
  1221. if s=featurestr[i] then
  1222. begin
  1223. include(features,i);
  1224. exit;
  1225. end;
  1226. result:=false;
  1227. end;
  1228. function SetMinFPConstPrec(const s: string; var a: tfloattype) : boolean;
  1229. var
  1230. value, error: longint;
  1231. begin
  1232. if (upper(s)='DEFAULT') then
  1233. begin
  1234. a:=s32real;
  1235. result:=true;
  1236. exit;
  1237. end;
  1238. result:=false;
  1239. val(s,value,error);
  1240. if (error<>0) then
  1241. exit;
  1242. case value of
  1243. 32: a:=s32real;
  1244. 64: a:=s64real;
  1245. { adding support for 80 bit here is tricky, since we can't really }
  1246. { check whether the target cpu+OS actually supports it }
  1247. else
  1248. exit;
  1249. end;
  1250. result:=true;
  1251. end;
  1252. function var_align(want_align: longint): shortint;
  1253. begin
  1254. var_align := used_align(want_align,current_settings.alignment.varalignmin,current_settings.alignment.varalignmax);
  1255. end;
  1256. function var_align_size(siz: longint): shortint;
  1257. begin
  1258. siz := size_2_align(siz);
  1259. var_align_size := var_align(siz);
  1260. end;
  1261. function const_align(want_align: longint): shortint;
  1262. begin
  1263. const_align := used_align(want_align,current_settings.alignment.constalignmin,current_settings.alignment.constalignmax);
  1264. end;
  1265. function const_align_size(siz: longint): shortint;
  1266. begin
  1267. siz := size_2_align(siz);
  1268. const_align_size := const_align(siz);
  1269. end;
  1270. {$ifdef ARM}
  1271. function is_double_hilo_swapped: boolean;{$ifdef USEINLINE}inline;{$endif}
  1272. begin
  1273. result := (current_settings.fputype in [fpu_fpa,fpu_fpa10,fpu_fpa11]) and
  1274. not(cs_fp_emulation in current_settings.moduleswitches);
  1275. {$ifdef FPC_DOUBLE_HILO_SWAPPED}
  1276. { inverse result if compiler was compiled with swapped hilo already }
  1277. result := not result;
  1278. {$endif FPC_DOUBLE_HILO_SWAPPED}
  1279. end;
  1280. {$endif ARM}
  1281. function floating_point_range_check_error : boolean;
  1282. begin
  1283. result:=cs_ieee_errors in current_settings.localswitches;
  1284. end;
  1285. {****************************************************************************
  1286. Init
  1287. ****************************************************************************}
  1288. {$ifdef unix}
  1289. {$define need_path_search}
  1290. {$endif unix}
  1291. {$ifdef os2}
  1292. {$define need_path_search}
  1293. {$endif os2}
  1294. {$ifdef macos}
  1295. {$define need_path_search}
  1296. {$endif macos}
  1297. procedure get_exepath;
  1298. var
  1299. localExepath : TCmdStr;
  1300. exeName:TCmdStr;
  1301. {$ifdef need_path_search}
  1302. hs1 : TPathStr;
  1303. {$endif need_path_search}
  1304. begin
  1305. localexepath:=GetEnvironmentVariable('PPC_EXEC_PATH');
  1306. if localexepath='' then
  1307. begin
  1308. exeName := FixFileName(system.paramstr(0));
  1309. localexepath := ExtractFilePath(exeName);
  1310. end;
  1311. {$ifdef need_path_search}
  1312. if localexepath='' then
  1313. begin
  1314. hs1 := ExtractFileName(exeName);
  1315. ChangeFileExt(hs1,source_info.exeext);
  1316. {$ifdef macos}
  1317. FindFile(hs1,GetEnvironmentVariable('Commands'),false,localExepath);
  1318. {$else macos}
  1319. FindFile(hs1,GetEnvironmentVariable('PATH'),false,localExepath);
  1320. {$endif macos}
  1321. localExepath:=ExtractFilePath(localExepath);
  1322. end;
  1323. {$endif need_path_search}
  1324. exepath:=FixPath(localExepath,false);
  1325. end;
  1326. procedure DoneGlobals;
  1327. begin
  1328. librarysearchpath.Free;
  1329. unitsearchpath.Free;
  1330. objectsearchpath.Free;
  1331. includesearchpath.Free;
  1332. frameworksearchpath.Free;
  1333. LinkLibraryAliases.Free;
  1334. LinkLibraryOrder.Free;
  1335. end;
  1336. procedure InitGlobals;
  1337. begin
  1338. get_exepath;
  1339. { reset globals }
  1340. do_build:=false;
  1341. do_release:=false;
  1342. do_make:=true;
  1343. compile_level:=0;
  1344. codegenerror:=false;
  1345. DLLsource:=false;
  1346. paratarget:=system_none;
  1347. paratargetasm:=as_none;
  1348. paratargetdbg:=dbg_none;
  1349. { Output }
  1350. OutputFileName:='';
  1351. OutputPrefix:=Nil;
  1352. OutputSuffix:=Nil;
  1353. OutputExeDir:='';
  1354. OutputUnitDir:='';
  1355. { Utils directory }
  1356. utilsdirectory:='';
  1357. utilsprefix:='';
  1358. cshared:=false;
  1359. rlinkpath:='';
  1360. sysrootpath:='';
  1361. { Search Paths }
  1362. librarysearchpath:=TSearchPathList.Create;
  1363. unitsearchpath:=TSearchPathList.Create;
  1364. includesearchpath:=TSearchPathList.Create;
  1365. objectsearchpath:=TSearchPathList.Create;
  1366. frameworksearchpath:=TSearchPathList.Create;
  1367. { Def file }
  1368. usewindowapi:=false;
  1369. description:='Compiled by FPC '+version_string+' - '+target_cpu_string;
  1370. DescriptionSetExplicity:=false;
  1371. SetPEFlagsSetExplicity:=false;
  1372. ImageBaseSetExplicity:=false;
  1373. MinStackSizeSetExplicity:=false;
  1374. MaxStackSizeSetExplicity:=false;
  1375. dllversion:='';
  1376. dllmajor:=1;
  1377. dllminor:=0;
  1378. dllrevision:=0;
  1379. nwscreenname := '';
  1380. nwthreadname := '';
  1381. nwcopyright := '';
  1382. UseDeffileForExports:=false;
  1383. UseDeffileForExportsSetExplicitly:=false;
  1384. GenerateImportSection:=false;
  1385. RelocSection:=false;
  1386. RelocSectionSetExplicitly:=false;
  1387. LinkTypeSetExplicitly:=false;
  1388. { memory sizes, will be overridden by parameter or default for target
  1389. in options or init_parser }
  1390. stacksize:=0;
  1391. { not initialized yet }
  1392. jmp_buf_size:=-1;
  1393. apptype:=app_cui;
  1394. { Init values }
  1395. init_settings:=default_settings;
  1396. if init_settings.optimizecputype=cpu_none then
  1397. init_settings.optimizecputype:=init_settings.cputype;
  1398. LinkLibraryAliases :=TLinkStrMap.Create;
  1399. LinkLibraryOrder :=TLinkStrMap.Create;
  1400. { enable all features by default }
  1401. features:=[low(Tfeature)..high(Tfeature)];
  1402. end;
  1403. {$ifdef windows}
  1404. initialization
  1405. finalization
  1406. if CFGDLLHandle<>0 then
  1407. FreeLibrary(CFGDllHandle);
  1408. {$endif windows}
  1409. end.