globals.pas 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671
  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. x86memorymodel : tx86memorymodel;
  133. { CPU targets with microcontroller support can add a controller specific unit }
  134. {$if defined(ARM) or defined(AVR)}
  135. controllertype : tcontrollertype;
  136. {$endif defined(ARM) or defined(AVR)}
  137. { WARNING: this pointer cannot be written as such in record token }
  138. pmessage : pmessagestaterecord;
  139. end;
  140. const
  141. LinkMapWeightDefault = 1000;
  142. type
  143. TLinkRec = record
  144. Key : AnsiString;
  145. Value : AnsiString; // key expands to valuelist "value"
  146. Weight: longint;
  147. end;
  148. TLinkStrMap = class
  149. private
  150. itemcnt : longint;
  151. fmap : Array Of TLinkRec;
  152. function Lookup(key:Ansistring):longint;
  153. function getlinkrec(i:longint):TLinkRec;
  154. public
  155. procedure Add(key:ansistring;value:AnsiString='';weight:longint=LinkMapWeightDefault);
  156. procedure addseries(keys:AnsiString;weight:longint=LinkMapWeightDefault);
  157. function AddDep(keyvalue:String):boolean;
  158. function AddWeight(keyvalue:String):boolean;
  159. procedure SetValue(key:AnsiString;Weight:Integer);
  160. procedure SortonWeight;
  161. function Find(key:AnsiString):AnsiString;
  162. procedure Expand(src:TCmdStrList;dest: TLinkStrMap);
  163. procedure UpdateWeights(Weightmap:TLinkStrMap);
  164. constructor Create;
  165. property count : longint read itemcnt;
  166. property items[I:longint]:TLinkRec read getlinkrec; default;
  167. end;
  168. tpendingstate = record
  169. nextverbositystr : shortstring;
  170. nextlocalswitches : tlocalswitches;
  171. nextverbosityfullswitch: longint;
  172. nextcallingstr : shortstring;
  173. nextmessagerecord : pmessagestaterecord;
  174. verbosityfullswitched,
  175. localswitcheschanged : boolean;
  176. end;
  177. var
  178. { specified inputfile }
  179. inputfilepath : string;
  180. inputfilename : string;
  181. { specified outputfile with -o parameter }
  182. outputfilename : string;
  183. outputprefix : pshortstring;
  184. outputsuffix : pshortstring;
  185. { specified with -FE or -FU }
  186. outputexedir : TPathStr;
  187. outputunitdir : TPathStr;
  188. { specified with -FW and -Fw }
  189. wpofeedbackinput,
  190. wpofeedbackoutput : TPathStr;
  191. { things specified with parameters }
  192. paratarget : tsystem;
  193. paratargetdbg : tdbg;
  194. paratargetasm : tasm;
  195. paralinkoptions : TCmdStr;
  196. paradynamiclinker : string;
  197. paraprintnodetree : byte;
  198. parapreprocess : boolean;
  199. printnodefile : text;
  200. { typical cross compiling params}
  201. { directory where the utils can be found (options -FD) }
  202. utilsdirectory : TPathStr;
  203. { targetname specific prefix used by these utils (options -XP<path>) }
  204. utilsprefix : TCmdStr;
  205. cshared : boolean; { pass --shared to ld to link C libs shared}
  206. Dontlinkstdlibpath: Boolean; { Don't add std paths to linkpath}
  207. rlinkpath : TCmdStr; { rpath-link linkdir override}
  208. sysrootpath : TCmdStr; { target system root to search dyn linker }
  209. { some flags for global compiler switches }
  210. do_build,
  211. do_release,
  212. do_make : boolean;
  213. { path for searching units, different paths can be seperated by ; }
  214. exepath : TPathStr; { Path to ppc }
  215. librarysearchpath,
  216. unitsearchpath,
  217. objectsearchpath,
  218. includesearchpath,
  219. frameworksearchpath : TSearchPathList;
  220. autoloadunits : string;
  221. { linking }
  222. usegnubinutils : boolean;
  223. forceforwardslash : boolean;
  224. usewindowapi : boolean;
  225. description : string;
  226. SetPEFlagsSetExplicity,
  227. SetPEOptFlagsSetExplicity,
  228. ImageBaseSetExplicity,
  229. MinStackSizeSetExplicity,
  230. MaxStackSizeSetExplicity,
  231. DescriptionSetExplicity : boolean;
  232. dllversion : string;
  233. dllmajor,
  234. dllminor,
  235. dllrevision : word; { revision only for netware }
  236. { win pe }
  237. peoptflags,
  238. peflags : longint;
  239. minstacksize,
  240. maxstacksize,
  241. imagebase : puint;
  242. UseDeffileForExports : boolean;
  243. UseDeffileForExportsSetExplicitly : boolean;
  244. GenerateImportSection,
  245. GenerateImportSectionSetExplicitly,
  246. RelocSection : boolean;
  247. MacOSXVersionMin,
  248. iPhoneOSVersionMin: string[15];
  249. RelocSectionSetExplicitly : boolean;
  250. LinkTypeSetExplicitly : boolean;
  251. current_tokenpos, { position of the last token }
  252. current_filepos : tfileposinfo; { current position }
  253. nwscreenname : string;
  254. nwthreadname : string;
  255. nwcopyright : string;
  256. codegenerror : boolean; { true if there is an error reported }
  257. block_type : tblock_type; { type of currently parsed block }
  258. compile_level : word;
  259. exceptblockcounter : integer; { each except block gets a unique number check gotos }
  260. current_exceptblock : integer; { the exceptblock number of the current block (0 if none) }
  261. LinkLibraryAliases : TLinkStrMap;
  262. LinkLibraryOrder : TLinkStrMap;
  263. init_settings,
  264. current_settings : tsettings;
  265. pendingstate : tpendingstate;
  266. { Memory sizes }
  267. heapsize,
  268. stacksize,
  269. jmp_buf_size,
  270. jmp_buf_align : longint;
  271. {$Ifdef EXTDEBUG}
  272. { parameter switches }
  273. debugstop : boolean;
  274. {$EndIf EXTDEBUG}
  275. { Application type (platform specific)
  276. see globtype.pas for description }
  277. apptype : tapptype;
  278. features : tfeatures;
  279. { prefix added to automatically generated setters/getters. If empty,
  280. no getters/setters will be automatically generated except if required
  281. for visibility reasons (but in that case the names will be mangled so
  282. they are unique) }
  283. prop_auto_getter_prefix,
  284. prop_auto_setter_prefix : string;
  285. const
  286. DLLsource : boolean = false;
  287. { used to set all registers used for each global function
  288. this should dramatically decrease the number of
  289. recompilations needed PM }
  290. simplify_ppu : boolean = true;
  291. Inside_asm_statement : boolean = false;
  292. global_unit_count : word = 0;
  293. { for error info in pp.pas }
  294. parser_current_file : string = '';
  295. {$if defined(m68k) or defined(arm)}
  296. { PalmOS resources }
  297. palmos_applicationname : string = 'FPC Application';
  298. palmos_applicationid : string[4] = 'FPCA';
  299. {$endif defined(m68k) or defined(arm)}
  300. {$ifdef powerpc}
  301. { default calling convention used on MorphOS }
  302. syscall_convention : string = 'LEGACY';
  303. {$endif powerpc}
  304. { default name of the C-style "main" procedure of the library/program }
  305. { (this will be prefixed with the target_info.cprefix) }
  306. defaultmainaliasname = 'main';
  307. mainaliasname : string = defaultmainaliasname;
  308. const
  309. default_settings : TSettings = (
  310. alignment : (
  311. procalign : 0;
  312. loopalign : 0;
  313. jumpalign : 0;
  314. constalignmin : 0;
  315. constalignmax : 0;
  316. varalignmin : 0;
  317. varalignmax : 0;
  318. localalignmin : 0;
  319. localalignmax : 0;
  320. recordalignmin : 0;
  321. recordalignmax : 0;
  322. maxCrecordalign : 0;
  323. );
  324. globalswitches : [cs_check_unit_name,cs_link_static];
  325. targetswitches : [];
  326. moduleswitches : [cs_extsyntax,cs_implicit_exceptions];
  327. localswitches : [cs_check_io,cs_typed_const_writable,cs_pointermath];
  328. modeswitches : fpcmodeswitches;
  329. optimizerswitches : [];
  330. genwpoptimizerswitches : [];
  331. dowpoptimizerswitches : [];
  332. debugswitches : [];
  333. setalloc : 0;
  334. packenum : 4;
  335. packrecords : 0;
  336. maxfpuregisters : 0;
  337. { Note: GENERIC_CPU is sued together with generic subdirectory to
  338. be able to compile some of the units without any real CPU.
  339. This is used to generate a CPU independant PPUDUMP utility. PM }
  340. {$ifdef GENERIC_CPU}
  341. cputype : cpu_none;
  342. optimizecputype : cpu_none;
  343. fputype : fpu_none;
  344. {$else not GENERIC_CPU}
  345. {$ifdef i386}
  346. cputype : cpu_Pentium;
  347. optimizecputype : cpu_Pentium3;
  348. fputype : fpu_x87;
  349. {$endif i386}
  350. {$ifdef m68k}
  351. cputype : cpu_MC68020;
  352. optimizecputype : cpu_MC68020;
  353. fputype : fpu_soft;
  354. {$endif m68k}
  355. {$ifdef powerpc}
  356. cputype : cpu_PPC604;
  357. optimizecputype : cpu_ppc7400;
  358. fputype : fpu_standard;
  359. {$endif powerpc}
  360. {$ifdef POWERPC64}
  361. cputype : cpu_PPC970;
  362. optimizecputype : cpu_ppc970;
  363. fputype : fpu_standard;
  364. {$endif POWERPC64}
  365. {$ifdef sparc}
  366. cputype : cpu_SPARC_V9;
  367. optimizecputype : cpu_SPARC_V9;
  368. fputype : fpu_hard;
  369. {$endif sparc}
  370. {$ifdef arm}
  371. cputype : cpu_armv3;
  372. optimizecputype : cpu_armv3;
  373. fputype : fpu_fpa;
  374. {$endif arm}
  375. {$ifdef x86_64}
  376. cputype : cpu_athlon64;
  377. optimizecputype : cpu_athlon64;
  378. fputype : fpu_sse64;
  379. {$endif x86_64}
  380. {$ifdef ia64}
  381. cputype : cpu_itanium;
  382. optimizecputype : cpu_itanium;
  383. fputype : fpu_itanium;
  384. {$endif ia64}
  385. {$ifdef avr}
  386. cputype : cpuinfo.cpu_avr5;
  387. optimizecputype : cpuinfo.cpu_avr5;
  388. fputype : fpu_none;
  389. {$endif avr}
  390. {$ifdef mips}
  391. cputype : cpu_mips2;
  392. optimizecputype : cpu_mips2;
  393. fputype : fpu_mips2;
  394. {$endif mips}
  395. {$ifdef jvm}
  396. cputype : cpu_none;
  397. optimizecputype : cpu_none;
  398. fputype : fpu_standard;
  399. {$endif jvm}
  400. {$ifdef aarch64}
  401. cputype : cpu_armv8;
  402. optimizecputype : cpu_armv8;
  403. fputype : fpu_vfp;
  404. {$endif aarch64}
  405. {$ifdef i8086}
  406. cputype : cpu_8086;
  407. optimizecputype : cpu_8086;
  408. fputype : fpu_x87;
  409. {$endif i8086}
  410. {$endif not GENERIC_CPU}
  411. asmmode : asmmode_standard;
  412. {$ifndef jvm}
  413. interfacetype : it_interfacecom;
  414. {$else jvm}
  415. interfacetype : it_interfacejava;
  416. {$endif jvm}
  417. defproccall : pocall_default;
  418. sourcecodepage : 28591;
  419. minfpconstprec : s32real;
  420. disabledircache : false;
  421. x86memorymodel : mm_small;
  422. {$if defined(ARM) or defined(AVR)}
  423. controllertype : ct_none;
  424. {$endif defined(ARM) or defined(AVR)}
  425. pmessage : nil;
  426. );
  427. var
  428. starttime : real;
  429. function getdatestr:string;
  430. function gettimestr:string;
  431. function filetimestring( t : longint) : string;
  432. function getrealtime : real;
  433. procedure DefaultReplacements(var s:ansistring);
  434. function GetEnvPChar(const envname:ansistring):pchar;
  435. procedure FreeEnvPChar(p:pchar);
  436. function is_number_float(d : double) : boolean;
  437. { discern +0.0 and -0.0 }
  438. function get_real_sign(r: bestreal): longint;
  439. procedure InitGlobals;
  440. procedure DoneGlobals;
  441. function string2guid(const s: string; var GUID: TGUID): boolean;
  442. function guid2string(const GUID: TGUID): string;
  443. function SetAktProcCall(const s:string; var a:tproccalloption):boolean;
  444. function Setabitype(const s:string;var a:tabi):boolean;
  445. function Setcputype(const s:string;var a:tcputype):boolean;
  446. function SetFpuType(const s:string;var a:tfputype):boolean;
  447. {$if defined(arm) or defined(avr)}
  448. function SetControllerType(const s:string;var a:tcontrollertype):boolean;
  449. {$endif defined(arm) or defined(avr)}
  450. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  451. function UpdateOptimizerStr(s:string;var a:toptimizerswitches):boolean;
  452. function UpdateWpoStr(s: string; var a: twpoptimizerswitches): boolean;
  453. function UpdateDebugStr(s:string;var a:tdebugswitches):boolean;
  454. function UpdateTargetSwitchStr(s: string; var a: ttargetswitches; global: boolean): boolean;
  455. function IncludeFeature(const s : string) : boolean;
  456. function SetMinFPConstPrec(const s: string; var a: tfloattype) : boolean;
  457. {# Routine to get the required alignment for size of data, which will
  458. be placed in bss segment, according to the current alignment requirements }
  459. function var_align(want_align: longint): shortint;
  460. function var_align_size(siz: longint): shortint;
  461. {# Routine to get the required alignment for size of data, which will
  462. be placed in data/const segment, according to the current alignment requirements }
  463. function const_align(want_align: longint): shortint;
  464. function const_align_size(siz: longint): shortint;
  465. {$ifdef ARM}
  466. function is_double_hilo_swapped: boolean;{$ifdef USEINLINE}inline;{$endif}
  467. {$endif ARM}
  468. function floating_point_range_check_error : boolean;
  469. { hide Sysutils.ExecuteProcess in units using this one after SysUtils}
  470. const
  471. ExecuteProcess = 'Do not use' deprecated 'Use cfileutil.RequotedExecuteProcess instead, ExecuteProcess cannot deal with single quotes as used by Unix command lines';
  472. implementation
  473. uses
  474. {$ifdef macos}
  475. macutils,
  476. {$endif}
  477. {$ifdef mswindows}
  478. {$ifdef VER2_4}
  479. cwindirs,
  480. {$else VER2_4}
  481. windirs,
  482. {$endif VER2_4}
  483. {$endif}
  484. comphook;
  485. {****************************************************************************
  486. TLinkStrMap
  487. ****************************************************************************}
  488. Constructor TLinkStrMap.create;
  489. begin
  490. inherited;
  491. itemcnt:=0;
  492. end;
  493. procedure TLinkStrMap.Add(key:ansistring;value:AnsiString='';weight:longint=LinkMapWeightDefault);
  494. begin
  495. if lookup(key)<>-1 Then
  496. exit;
  497. if itemcnt<=length(fmap) Then
  498. setlength(fmap,itemcnt+10);
  499. fmap[itemcnt].key:=key;
  500. fmap[itemcnt].value:=value;
  501. fmap[itemcnt].weight:=weight;
  502. inc(itemcnt);
  503. end;
  504. function TLinkStrMap.AddDep(keyvalue:String):boolean;
  505. var
  506. i : Longint;
  507. begin
  508. AddDep:=false;
  509. i:=pos('=',keyvalue);
  510. if i=0 then
  511. exit;
  512. Add(Copy(KeyValue,1,i-1),Copy(KeyValue,i+1,length(KeyValue)-i));
  513. AddDep:=True;
  514. end;
  515. function TLinkStrMap.AddWeight(keyvalue:String):boolean;
  516. var
  517. i,j : Longint;
  518. Code : Word;
  519. s : AnsiString;
  520. begin
  521. AddWeight:=false;
  522. i:=pos('=',keyvalue);
  523. if i=0 then
  524. exit;
  525. s:=Copy(KeyValue,i+1,length(KeyValue)-i);
  526. val(s,j,code);
  527. if code=0 Then
  528. begin
  529. Add(Copy(KeyValue,1,i-1),'',j);
  530. AddWeight:=True;
  531. end;
  532. end;
  533. procedure TLinkStrMap.addseries(keys:AnsiString;weight:longint);
  534. var
  535. i,j,k : longint;
  536. begin
  537. k:=length(keys);
  538. i:=1;
  539. while i<=k do
  540. begin
  541. j:=i;
  542. while (i<=k) and (keys[i]<>',') do
  543. inc(i);
  544. add(copy(keys,j,i-j),'',weight);
  545. inc(i);
  546. end;
  547. end;
  548. procedure TLinkStrMap.SetValue(Key:Ansistring;weight:Integer);
  549. var
  550. j : longint;
  551. begin
  552. j:=lookup(key);
  553. if j<>-1 then
  554. fmap[j].weight:=weight;
  555. end;
  556. function TLinkStrMap.find(key:Ansistring):Ansistring;
  557. var
  558. j : longint;
  559. begin
  560. find:='';
  561. j:=lookup(key);
  562. if j<>-1 then
  563. find:=fmap[j].value;
  564. end;
  565. function TLinkStrMap.lookup(key:Ansistring):longint;
  566. var
  567. i : longint;
  568. begin
  569. lookup:=-1;
  570. i:=0;
  571. while (i<itemcnt) and (fmap[i].key<>key) do
  572. inc(i);
  573. if i<>itemcnt then
  574. lookup:=i;
  575. end;
  576. procedure TLinkStrMap.SortOnWeight;
  577. var
  578. i, j : longint;
  579. m : TLinkRec;
  580. begin
  581. if itemcnt <2 then exit;
  582. for i:=0 to itemcnt-1 do
  583. for j:=i+1 to itemcnt-1 do
  584. begin
  585. if fmap[i].weight>fmap[j].weight Then
  586. begin
  587. m:=fmap[i];
  588. fmap[i]:=fmap[j];
  589. fmap[j]:=m;
  590. end;
  591. end;
  592. end;
  593. function TLinkStrMap.getlinkrec(i:longint):TLinkRec;
  594. begin
  595. result:=fmap[i];
  596. end;
  597. procedure TLinkStrMap.Expand(Src:TCmdStrList;Dest:TLinkStrMap);
  598. // expands every thing in Src to Dest for linkorder purposes.
  599. var
  600. r : longint;
  601. LibN : TCmdStr;
  602. begin
  603. while not src.empty do
  604. begin
  605. LibN:=src.getfirst;
  606. r:=lookup (LibN);
  607. if r=-1 then
  608. dest.add(LibN)
  609. else
  610. dest.addseries(fmap[r].value);
  611. end;
  612. end;
  613. procedure TLinkStrMap.UpdateWeights(Weightmap:TLinkStrMap);
  614. var
  615. l,r : longint;
  616. begin
  617. for l := 0 to itemcnt-1 do
  618. begin
  619. r:=weightmap.lookup (fmap[l].key);
  620. if r<>-1 then
  621. fmap[l].weight:=weightmap[r].weight;
  622. end;
  623. end;
  624. {****************************************************************************
  625. Time Handling
  626. ****************************************************************************}
  627. Function L0(l:longint):string;
  628. {
  629. return the string of value l, if l<10 then insert a zero, so
  630. the string is always at least 2 chars '01','02',etc
  631. }
  632. var
  633. s : string;
  634. begin
  635. Str(l,s);
  636. if l<10 then
  637. s:='0'+s;
  638. L0:=s;
  639. end;
  640. function gettimestr:string;
  641. {
  642. get the current time in a string HH:MM:SS
  643. }
  644. var
  645. hour,min,sec,hsec : word;
  646. begin
  647. DecodeTime(Time,hour,min,sec,hsec);
  648. gettimestr:=L0(Hour)+':'+L0(min)+':'+L0(sec);
  649. end;
  650. function getdatestr:string;
  651. {
  652. get the current date in a string YY/MM/DD
  653. }
  654. var
  655. Year,Month,Day: Word;
  656. begin
  657. DecodeDate(Date,year,month,day);
  658. getdatestr:=L0(Year)+'/'+L0(Month)+'/'+L0(Day);
  659. end;
  660. function filetimestring( t : longint) : string;
  661. {
  662. convert dos datetime t to a string YY/MM/DD HH:MM:SS
  663. }
  664. var
  665. DT : TDateTime;
  666. hsec : word;
  667. Year,Month,Day: Word;
  668. hour,min,sec : word;
  669. begin
  670. if t=-1 then
  671. begin
  672. Result := 'Not Found';
  673. exit;
  674. end;
  675. DT := FileDateToDateTime(t);
  676. DecodeTime(DT,hour,min,sec,hsec);
  677. DecodeDate(DT,year,month,day);
  678. Result := L0(Year)+'/'+L0(Month)+'/'+L0(Day)+' '+L0(Hour)+':'+L0(min)+':'+L0(sec);
  679. end;
  680. function getrealtime : real;
  681. var
  682. h,m,s,s1000 : word;
  683. begin
  684. DecodeTime(Time,h,m,s,s1000);
  685. result:=h*3600.0+m*60.0+s+s1000/1000.0;
  686. end;
  687. {****************************************************************************
  688. Default Macro Handling
  689. ****************************************************************************}
  690. procedure DefaultReplacements(var s:ansistring);
  691. {$ifdef mswindows}
  692. procedure ReplaceSpecialFolder(const MacroName: string; const ID: integer);
  693. begin
  694. // Only try to receive the special folders (and thus dynamically
  695. // load shfolder.dll) when that's needed.
  696. if pos(MacroName,s)>0 then
  697. Replace(s,MacroName,GetWindowsSpecialDir(ID));
  698. end;
  699. {$endif mswindows}
  700. var
  701. envstr: string;
  702. envvalue: pchar;
  703. i: integer;
  704. begin
  705. { Replace some macros }
  706. Replace(s,'$FPCVERSION',version_string);
  707. Replace(s,'$FPCFULLVERSION',full_version_string);
  708. Replace(s,'$FPCDATE',date_string);
  709. Replace(s,'$FPCCPU',target_cpu_string);
  710. Replace(s,'$FPCOS',target_os_string);
  711. if (tf_use_8_3 in Source_Info.Flags) or
  712. (tf_use_8_3 in Target_Info.Flags) then
  713. Replace(s,'$FPCTARGET',target_os_string)
  714. else
  715. Replace(s,'$FPCTARGET',target_full_string);
  716. {$ifdef mswindows}
  717. ReplaceSpecialFolder('$LOCAL_APPDATA',CSIDL_LOCAL_APPDATA);
  718. ReplaceSpecialFolder('$APPDATA',CSIDL_APPDATA);
  719. ReplaceSpecialFolder('$COMMON_APPDATA',CSIDL_COMMON_APPDATA);
  720. ReplaceSpecialFolder('$PERSONAL',CSIDL_PERSONAL);
  721. ReplaceSpecialFolder('$PROGRAM_FILES',CSIDL_PROGRAM_FILES);
  722. ReplaceSpecialFolder('$PROGRAM_FILES_COMMON',CSIDL_PROGRAM_FILES_COMMON);
  723. ReplaceSpecialFolder('$PROFILE',CSIDL_PROFILE);
  724. {$endif mswindows}
  725. { Replace environment variables between dollar signs }
  726. i := pos('$',s);
  727. while i>0 do
  728. begin
  729. envstr:=copy(s,i+1,length(s)-i);
  730. i:=pos('$',envstr);
  731. if i>0 then
  732. begin
  733. envstr := copy(envstr,1,i-1);
  734. envvalue := GetEnvPChar(envstr);
  735. if assigned(envvalue) then
  736. begin
  737. Replace(s,'$'+envstr+'$',envvalue);
  738. // Look if there is another env.var in the string
  739. i:=pos('$',s);
  740. end
  741. else
  742. // if the env.var is not set, do not replace the env.variable
  743. // and stop looking for more env.var within the string
  744. i := 0;
  745. FreeEnvPChar(envvalue);
  746. end;
  747. end;
  748. end;
  749. {****************************************************************************
  750. OS Dependent things
  751. ****************************************************************************}
  752. function GetEnvPChar(const envname:ansistring):pchar;
  753. {$ifdef mswindows}
  754. var
  755. s : string;
  756. i,len : longint;
  757. hp,p,p2 : pchar;
  758. {$endif}
  759. begin
  760. {$ifdef hasunix}
  761. GetEnvPchar:=BaseUnix.fpGetEnv(pansichar(envname));
  762. {$define GETENVOK}
  763. {$endif}
  764. {$ifdef mswindows}
  765. GetEnvPchar:=nil;
  766. p:=GetEnvironmentStrings;
  767. hp:=p;
  768. while hp^<>#0 do
  769. begin
  770. s:=strpas(hp);
  771. i:=pos('=',s);
  772. len:=strlen(hp);
  773. if upper(copy(s,1,i-1))=upper(envname) then
  774. begin
  775. GetMem(p2,len-length(envname));
  776. Move(hp[i],p2^,len-length(envname));
  777. GetEnvPchar:=p2;
  778. break;
  779. end;
  780. { next string entry}
  781. hp:=hp+len+1;
  782. end;
  783. FreeEnvironmentStrings(p);
  784. {$define GETENVOK}
  785. {$endif}
  786. {$ifdef os2}
  787. GetEnvPChar := Dos.GetEnvPChar (EnvName);
  788. {$define GETENVOK}
  789. {$endif}
  790. {$ifdef GETENVOK}
  791. {$undef GETENVOK}
  792. {$else}
  793. GetEnvPchar:=StrPNew(GetEnvironmentVariable(envname));
  794. if (length(GetEnvPChar)=0) then
  795. begin
  796. FreeEnvPChar(GetEnvPChar);
  797. GetEnvPChar:=nil;
  798. end;
  799. {$endif}
  800. end;
  801. procedure FreeEnvPChar(p:pchar);
  802. begin
  803. {$ifndef hasunix}
  804. {$ifndef os2}
  805. freemem(p);
  806. {$endif}
  807. {$endif}
  808. end;
  809. {$if defined(MORPHOS) or defined(AMIGA)}
  810. {$define AMIGASHELL}
  811. {$endif}
  812. {$UNDEF AMIGASHELL}
  813. function is_number_float(d : double) : boolean;
  814. var
  815. bytearray : array[0..7] of byte;
  816. begin
  817. move(d,bytearray,8);
  818. { only 1.1 save, 1.0.x will use always little endian }
  819. {$ifdef FPC_BIG_ENDIAN}
  820. result:=((bytearray[0] and $7f)<>$7f) or ((bytearray[1] and $f0)<>$f0);
  821. {$else FPC_BIG_ENDIAN}
  822. result:=((bytearray[7] and $7f)<>$7f) or ((bytearray[6] and $f0)<>$f0);
  823. {$endif FPC_BIG_ENDIAN}
  824. end;
  825. function get_real_sign(r: bestreal): longint;
  826. var
  827. p: pbyte;
  828. begin
  829. p := pbyte(@r);
  830. {$ifdef CPU_ARM}
  831. inc(p,4);
  832. {$else}
  833. {$ifdef FPC_LITTLE_ENDIAN}
  834. inc(p,sizeof(r)-1);
  835. {$endif}
  836. {$endif}
  837. if (p^ and $80) = 0 then
  838. result := 1
  839. else
  840. result := -1;
  841. end;
  842. function convertdoublerec(d : tdoublerec) : tdoublerec;{$ifdef USEINLINE}inline;{$endif}
  843. {$ifdef CPUARM}
  844. var
  845. i : longint;
  846. begin
  847. for i:=0 to 3 do
  848. begin
  849. result.bytes[i+4]:=d.bytes[i];
  850. result.bytes[i]:=d.bytes[i+4];
  851. end;
  852. {$else CPUARM}
  853. begin
  854. result:=d;
  855. {$endif CPUARM}
  856. end;
  857. { '('D1:'00000000-'D2:'0000-'D3:'0000-'D4:'0000-000000000000)' }
  858. function string2guid(const s: string; var GUID: TGUID): boolean;
  859. function ishexstr(const hs: string): boolean;
  860. var
  861. i: integer;
  862. begin
  863. ishexstr:=false;
  864. for i:=1 to Length(hs) do begin
  865. if not (hs[i] in ['0'..'9','A'..'F','a'..'f']) then
  866. exit;
  867. end;
  868. ishexstr:=true;
  869. end;
  870. function hexstr2longint(const hexs: string): longint;
  871. var
  872. i: integer;
  873. rl: longint;
  874. begin
  875. rl:=0;
  876. for i:=1 to length(hexs) do begin
  877. rl:=rl shl 4;
  878. case hexs[i] of
  879. '0'..'9' : inc(rl,ord(hexs[i])-ord('0'));
  880. 'A'..'F' : inc(rl,ord(hexs[i])-ord('A')+10);
  881. 'a'..'f' : inc(rl,ord(hexs[i])-ord('a')+10);
  882. end
  883. end;
  884. hexstr2longint:=rl;
  885. end;
  886. var
  887. i: integer;
  888. begin
  889. if (Length(s)=38) and (s[1]='{') and (s[38]='}') and
  890. (s[10]='-') and (s[15]='-') and (s[20]='-') and (s[25]='-') and
  891. ishexstr(copy(s,2,8)) and ishexstr(copy(s,11,4)) and
  892. ishexstr(copy(s,16,4)) and ishexstr(copy(s,21,4)) and
  893. ishexstr(copy(s,26,12)) then begin
  894. GUID.D1:=dword(hexstr2longint(copy(s,2,8)));
  895. { these values are arealdy in the correct range (4 chars = word) }
  896. GUID.D2:=word(hexstr2longint(copy(s,11,4)));
  897. GUID.D3:=word(hexstr2longint(copy(s,16,4)));
  898. for i:=0 to 1 do
  899. GUID.D4[i]:=byte(hexstr2longint(copy(s,21+i*2,2)));
  900. for i:=2 to 7 do
  901. GUID.D4[i]:=byte(hexstr2longint(copy(s,22+i*2,2)));
  902. string2guid:=true;
  903. end
  904. else if (length(s)=0) then
  905. begin
  906. FillChar(GUID,SizeOf(GUID),0);
  907. string2guid:=true;
  908. end
  909. else
  910. string2guid:=false;
  911. end;
  912. function guid2string(const GUID: TGUID): string;
  913. begin
  914. guid2string:=
  915. '{'+hexstr(GUID.D1,8)+
  916. '-'+hexstr(GUID.D2,4)+
  917. '-'+hexstr(GUID.D3,4)+
  918. '-'+hexstr(GUID.D4[0],2)+hexstr(GUID.D4[1],2)+
  919. '-'+hexstr(GUID.D4[2],2)+hexstr(GUID.D4[3],2)+
  920. hexstr(GUID.D4[4],2)+hexstr(GUID.D4[5],2)+
  921. hexstr(GUID.D4[6],2)+hexstr(GUID.D4[7],2)+
  922. '}';
  923. end;
  924. function SetAktProcCall(const s:string; var a:tproccalloption):boolean;
  925. const
  926. DefProcCallName : array[tproccalloption] of string[12] = ('',
  927. 'CDECL',
  928. 'CPPDECL',
  929. 'FAR16',
  930. 'OLDFPCCALL',
  931. '', { internproc }
  932. '', { syscall }
  933. 'PASCAL',
  934. 'REGISTER',
  935. 'SAFECALL',
  936. 'STDCALL',
  937. 'SOFTFLOAT',
  938. 'MWPASCAL',
  939. 'INTERRUPT'
  940. );
  941. var
  942. t : tproccalloption;
  943. hs : string;
  944. begin
  945. result:=false;
  946. if (s = '') then
  947. exit;
  948. hs:=upper(s);
  949. if (hs = 'DEFAULT') then
  950. begin
  951. a := pocall_default;
  952. result := true;
  953. exit;
  954. end;
  955. for t:=low(tproccalloption) to high(tproccalloption) do
  956. if DefProcCallName[t]=hs then
  957. begin
  958. a:=t;
  959. result:=true;
  960. break;
  961. end;
  962. end;
  963. function Setabitype(const s:string;var a:tabi):boolean;
  964. var
  965. t : tabi;
  966. hs : string;
  967. begin
  968. result:=false;
  969. hs:=Upper(s);
  970. for t:=low(t) to high(t) do
  971. if abiinfo[t].supported and
  972. (abiinfo[t].name=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 embedded_controllers[t].controllertypestr=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. Result:=Result and 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 UpdateTargetSwitchStr(s: string; var a: ttargetswitches; global: boolean): boolean;
  1216. var
  1217. tok,
  1218. value : string;
  1219. setstr: string[2];
  1220. equalspos: longint;
  1221. doset,
  1222. gotvalue,
  1223. found : boolean;
  1224. opt : ttargetswitch;
  1225. begin
  1226. result:=true;
  1227. repeat
  1228. tok:=GetToken(s,',');
  1229. if tok='' then
  1230. break;
  1231. setstr:=upper(copy(tok,length(tok),1));
  1232. if setstr='-' then
  1233. begin
  1234. setlength(tok,length(tok)-1);
  1235. doset:=false;
  1236. end
  1237. else
  1238. doset:=true;
  1239. { value specified? }
  1240. gotvalue:=false;
  1241. equalspos:=pos('=',tok);
  1242. if equalspos<>0 then
  1243. begin
  1244. value:=copy(tok,equalspos+1,length(tok));
  1245. delete(tok,equalspos,length(tok));
  1246. gotvalue:=true;
  1247. end;
  1248. found:=false;
  1249. uppervar(tok);
  1250. for opt:=low(ttargetswitch) to high(ttargetswitch) do
  1251. begin
  1252. if TargetSwitchStr[opt].name=tok then
  1253. begin
  1254. found:=true;
  1255. break;
  1256. end;
  1257. end;
  1258. if found then
  1259. begin
  1260. if not global and
  1261. TargetSwitchStr[opt].isglobal then
  1262. result:=false
  1263. else if not TargetSwitchStr[opt].hasvalue then
  1264. begin
  1265. if gotvalue then
  1266. result:=false;
  1267. if doset then
  1268. include(a,opt)
  1269. else
  1270. exclude(a,opt)
  1271. end
  1272. else
  1273. begin
  1274. if not gotvalue or
  1275. not doset then
  1276. result:=false
  1277. else
  1278. begin
  1279. case opt of
  1280. ts_auto_getter_prefix:
  1281. prop_auto_getter_prefix:=value;
  1282. ts_auto_setter_predix:
  1283. prop_auto_setter_prefix:=value;
  1284. else
  1285. begin
  1286. writeln('Internalerror 2012053001');
  1287. halt(1);
  1288. end;
  1289. end;
  1290. end;
  1291. end;
  1292. end
  1293. else
  1294. result:=false;
  1295. until false;
  1296. end;
  1297. function IncludeFeature(const s : string) : boolean;
  1298. var
  1299. i : tfeature;
  1300. begin
  1301. result:=true;
  1302. for i:=low(tfeature) to high(tfeature) do
  1303. if s=featurestr[i] then
  1304. begin
  1305. include(features,i);
  1306. exit;
  1307. end;
  1308. result:=false;
  1309. end;
  1310. function SetMinFPConstPrec(const s: string; var a: tfloattype) : boolean;
  1311. var
  1312. value, error: longint;
  1313. begin
  1314. if (upper(s)='DEFAULT') then
  1315. begin
  1316. a:=s32real;
  1317. result:=true;
  1318. exit;
  1319. end;
  1320. result:=false;
  1321. val(s,value,error);
  1322. if (error<>0) then
  1323. exit;
  1324. case value of
  1325. 32: a:=s32real;
  1326. 64: a:=s64real;
  1327. { adding support for 80 bit here is tricky, since we can't really }
  1328. { check whether the target cpu+OS actually supports it }
  1329. else
  1330. exit;
  1331. end;
  1332. result:=true;
  1333. end;
  1334. function var_align(want_align: longint): shortint;
  1335. begin
  1336. var_align := used_align(want_align,current_settings.alignment.varalignmin,current_settings.alignment.varalignmax);
  1337. end;
  1338. function var_align_size(siz: longint): shortint;
  1339. begin
  1340. siz := size_2_align(siz);
  1341. var_align_size := var_align(siz);
  1342. end;
  1343. function const_align(want_align: longint): shortint;
  1344. begin
  1345. const_align := used_align(want_align,current_settings.alignment.constalignmin,current_settings.alignment.constalignmax);
  1346. end;
  1347. function const_align_size(siz: longint): shortint;
  1348. begin
  1349. siz := size_2_align(siz);
  1350. const_align_size := const_align(siz);
  1351. end;
  1352. {$ifdef ARM}
  1353. function is_double_hilo_swapped: boolean;{$ifdef USEINLINE}inline;{$endif}
  1354. begin
  1355. result := (current_settings.fputype in [fpu_fpa,fpu_fpa10,fpu_fpa11]) and
  1356. not(cs_fp_emulation in current_settings.moduleswitches);
  1357. {$ifdef FPC_DOUBLE_HILO_SWAPPED}
  1358. { inverse result if compiler was compiled with swapped hilo already }
  1359. result := not result;
  1360. {$endif FPC_DOUBLE_HILO_SWAPPED}
  1361. end;
  1362. {$endif ARM}
  1363. function floating_point_range_check_error : boolean;
  1364. begin
  1365. result:=cs_ieee_errors in current_settings.localswitches;
  1366. end;
  1367. {****************************************************************************
  1368. Init
  1369. ****************************************************************************}
  1370. {$ifdef unix}
  1371. {$define need_path_search}
  1372. {$endif unix}
  1373. {$ifdef os2}
  1374. {$define need_path_search}
  1375. {$endif os2}
  1376. {$ifdef macos}
  1377. {$define need_path_search}
  1378. {$endif macos}
  1379. procedure get_exepath;
  1380. var
  1381. localExepath : TCmdStr;
  1382. exeName:TCmdStr;
  1383. {$ifdef need_path_search}
  1384. hs1 : TPathStr;
  1385. {$endif need_path_search}
  1386. begin
  1387. localexepath:=GetEnvironmentVariable('PPC_EXEC_PATH');
  1388. if localexepath='' then
  1389. begin
  1390. exeName := FixFileName(system.paramstr(0));
  1391. localexepath := ExtractFilePath(exeName);
  1392. end;
  1393. {$ifdef need_path_search}
  1394. if localexepath='' then
  1395. begin
  1396. hs1 := ExtractFileName(exeName);
  1397. ChangeFileExt(hs1,source_info.exeext);
  1398. {$ifdef macos}
  1399. FindFile(hs1,GetEnvironmentVariable('Commands'),false,localExepath);
  1400. {$else macos}
  1401. FindFile(hs1,GetEnvironmentVariable('PATH'),false,localExepath);
  1402. {$endif macos}
  1403. localExepath:=ExtractFilePath(localExepath);
  1404. end;
  1405. {$endif need_path_search}
  1406. exepath:=FixPath(localExepath,false);
  1407. end;
  1408. procedure DoneGlobals;
  1409. begin
  1410. librarysearchpath.Free;
  1411. unitsearchpath.Free;
  1412. objectsearchpath.Free;
  1413. includesearchpath.Free;
  1414. frameworksearchpath.Free;
  1415. LinkLibraryAliases.Free;
  1416. LinkLibraryOrder.Free;
  1417. end;
  1418. procedure InitGlobals;
  1419. begin
  1420. get_exepath;
  1421. { reset globals }
  1422. do_build:=false;
  1423. do_release:=false;
  1424. do_make:=true;
  1425. compile_level:=0;
  1426. codegenerror:=false;
  1427. DLLsource:=false;
  1428. paratarget:=system_none;
  1429. paratargetasm:=as_none;
  1430. paratargetdbg:=dbg_none;
  1431. { Output }
  1432. OutputFileName:='';
  1433. OutputPrefix:=Nil;
  1434. OutputSuffix:=Nil;
  1435. OutputExeDir:='';
  1436. OutputUnitDir:='';
  1437. { Utils directory }
  1438. utilsdirectory:='';
  1439. utilsprefix:='';
  1440. cshared:=false;
  1441. rlinkpath:='';
  1442. sysrootpath:='';
  1443. { Search Paths }
  1444. librarysearchpath:=TSearchPathList.Create;
  1445. unitsearchpath:=TSearchPathList.Create;
  1446. includesearchpath:=TSearchPathList.Create;
  1447. objectsearchpath:=TSearchPathList.Create;
  1448. frameworksearchpath:=TSearchPathList.Create;
  1449. { Def file }
  1450. usewindowapi:=false;
  1451. description:='Compiled by FPC '+version_string+' - '+target_cpu_string;
  1452. DescriptionSetExplicity:=false;
  1453. SetPEFlagsSetExplicity:=false;
  1454. SetPEOptFlagsSetExplicity:=false;
  1455. ImageBaseSetExplicity:=false;
  1456. MinStackSizeSetExplicity:=false;
  1457. MaxStackSizeSetExplicity:=false;
  1458. dllversion:='';
  1459. dllmajor:=1;
  1460. dllminor:=0;
  1461. dllrevision:=0;
  1462. nwscreenname := '';
  1463. nwthreadname := '';
  1464. nwcopyright := '';
  1465. UseDeffileForExports:=false;
  1466. UseDeffileForExportsSetExplicitly:=false;
  1467. GenerateImportSection:=false;
  1468. RelocSection:=false;
  1469. RelocSectionSetExplicitly:=false;
  1470. LinkTypeSetExplicitly:=false;
  1471. MacOSXVersionMin:='';
  1472. iPhoneOSVersionMin:='';
  1473. { memory sizes, will be overridden by parameter or default for target
  1474. in options or init_parser }
  1475. stacksize:=0;
  1476. { not initialized yet }
  1477. {$ifndef jvm}
  1478. jmp_buf_size:=-1;
  1479. {$else}
  1480. jmp_buf_size:=0;
  1481. {$endif}
  1482. apptype:=app_cui;
  1483. { Init values }
  1484. init_settings:=default_settings;
  1485. if init_settings.optimizecputype=cpu_none then
  1486. init_settings.optimizecputype:=init_settings.cputype;
  1487. LinkLibraryAliases :=TLinkStrMap.Create;
  1488. LinkLibraryOrder :=TLinkStrMap.Create;
  1489. { enable all features by default }
  1490. features:=[low(Tfeature)..high(Tfeature)];
  1491. end;
  1492. end.