globals.pas 49 KB

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