globals.pas 46 KB

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