globals.pas 49 KB

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