globals.pas 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  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. function is_number_float(d : double) : boolean;
  821. var
  822. bytearray : array[0..7] of byte;
  823. begin
  824. move(d,bytearray,8);
  825. { only 1.1 save, 1.0.x will use always little endian }
  826. {$ifdef FPC_BIG_ENDIAN}
  827. result:=((bytearray[0] and $7f)<>$7f) or ((bytearray[1] and $f0)<>$f0);
  828. {$else FPC_BIG_ENDIAN}
  829. result:=((bytearray[7] and $7f)<>$7f) or ((bytearray[6] and $f0)<>$f0);
  830. {$endif FPC_BIG_ENDIAN}
  831. end;
  832. function get_real_sign(r: bestreal): longint;
  833. var
  834. p: pbyte;
  835. begin
  836. p := pbyte(@r);
  837. {$ifdef CPU_ARM}
  838. inc(p,4);
  839. {$else}
  840. {$ifdef FPC_LITTLE_ENDIAN}
  841. inc(p,sizeof(r)-1);
  842. {$endif}
  843. {$endif}
  844. if (p^ and $80) = 0 then
  845. result := 1
  846. else
  847. result := -1;
  848. end;
  849. function convertdoublerec(d : tdoublerec) : tdoublerec;{$ifdef USEINLINE}inline;{$endif}
  850. {$ifdef CPUARM}
  851. var
  852. i : longint;
  853. begin
  854. for i:=0 to 3 do
  855. begin
  856. result.bytes[i+4]:=d.bytes[i];
  857. result.bytes[i]:=d.bytes[i+4];
  858. end;
  859. {$else CPUARM}
  860. begin
  861. result:=d;
  862. {$endif CPUARM}
  863. end;
  864. { '('D1:'00000000-'D2:'0000-'D3:'0000-'D4:'0000-000000000000)' }
  865. function string2guid(const s: string; var GUID: TGUID): boolean;
  866. function ishexstr(const hs: string): boolean;
  867. var
  868. i: integer;
  869. begin
  870. ishexstr:=false;
  871. for i:=1 to Length(hs) do begin
  872. if not (hs[i] in ['0'..'9','A'..'F','a'..'f']) then
  873. exit;
  874. end;
  875. ishexstr:=true;
  876. end;
  877. function hexstr2longint(const hexs: string): longint;
  878. var
  879. i: integer;
  880. rl: longint;
  881. begin
  882. rl:=0;
  883. for i:=1 to length(hexs) do begin
  884. rl:=rl shl 4;
  885. case hexs[i] of
  886. '0'..'9' : inc(rl,ord(hexs[i])-ord('0'));
  887. 'A'..'F' : inc(rl,ord(hexs[i])-ord('A')+10);
  888. 'a'..'f' : inc(rl,ord(hexs[i])-ord('a')+10);
  889. end
  890. end;
  891. hexstr2longint:=rl;
  892. end;
  893. var
  894. i: integer;
  895. begin
  896. if (Length(s)=38) and (s[1]='{') and (s[38]='}') and
  897. (s[10]='-') and (s[15]='-') and (s[20]='-') and (s[25]='-') and
  898. ishexstr(copy(s,2,8)) and ishexstr(copy(s,11,4)) and
  899. ishexstr(copy(s,16,4)) and ishexstr(copy(s,21,4)) and
  900. ishexstr(copy(s,26,12)) then begin
  901. GUID.D1:=dword(hexstr2longint(copy(s,2,8)));
  902. { these values are arealdy in the correct range (4 chars = word) }
  903. GUID.D2:=word(hexstr2longint(copy(s,11,4)));
  904. GUID.D3:=word(hexstr2longint(copy(s,16,4)));
  905. for i:=0 to 1 do
  906. GUID.D4[i]:=byte(hexstr2longint(copy(s,21+i*2,2)));
  907. for i:=2 to 7 do
  908. GUID.D4[i]:=byte(hexstr2longint(copy(s,22+i*2,2)));
  909. string2guid:=true;
  910. end
  911. else if (length(s)=0) then
  912. begin
  913. FillChar(GUID,SizeOf(GUID),0);
  914. string2guid:=true;
  915. end
  916. else
  917. string2guid:=false;
  918. end;
  919. function guid2string(const GUID: TGUID): string;
  920. begin
  921. guid2string:=
  922. '{'+hexstr(GUID.D1,8)+
  923. '-'+hexstr(GUID.D2,4)+
  924. '-'+hexstr(GUID.D3,4)+
  925. '-'+hexstr(GUID.D4[0],2)+hexstr(GUID.D4[1],2)+
  926. '-'+hexstr(GUID.D4[2],2)+hexstr(GUID.D4[3],2)+
  927. hexstr(GUID.D4[4],2)+hexstr(GUID.D4[5],2)+
  928. hexstr(GUID.D4[6],2)+hexstr(GUID.D4[7],2)+
  929. '}';
  930. end;
  931. function SetAktProcCall(const s:string; var a:tproccalloption):boolean;
  932. const
  933. DefProcCallName : array[tproccalloption] of string[12] = ('',
  934. 'CDECL',
  935. 'CPPDECL',
  936. 'FAR16',
  937. 'OLDFPCCALL',
  938. '', { internproc }
  939. '', { syscall }
  940. 'PASCAL',
  941. 'REGISTER',
  942. 'SAFECALL',
  943. 'STDCALL',
  944. 'SOFTFLOAT',
  945. 'MWPASCAL',
  946. 'INTERRUPT'
  947. );
  948. var
  949. t : tproccalloption;
  950. hs : string;
  951. begin
  952. result:=false;
  953. if (s = '') then
  954. exit;
  955. hs:=upper(s);
  956. if (hs = 'DEFAULT') then
  957. begin
  958. a := pocall_default;
  959. result := true;
  960. exit;
  961. end;
  962. for t:=low(tproccalloption) to high(tproccalloption) do
  963. if DefProcCallName[t]=hs then
  964. begin
  965. a:=t;
  966. result:=true;
  967. break;
  968. end;
  969. end;
  970. function Setabitype(const s:string;var a:tabi):boolean;
  971. var
  972. t : tabi;
  973. hs : string;
  974. begin
  975. result:=false;
  976. hs:=Upper(s);
  977. for t:=low(t) to high(t) do
  978. if abiinfo[t].supported and
  979. (abiinfo[t].name=hs) then
  980. begin
  981. a:=t;
  982. result:=true;
  983. break;
  984. end;
  985. end;
  986. function Setoptimizecputype(const s:string;var a:tcputype):boolean;
  987. var
  988. t : tcputype;
  989. hs : string;
  990. begin
  991. result:=false;
  992. hs:=Upper(s);
  993. for t:=low(tcputype) to high(tcputype) do
  994. if cputypestr[t]=hs then
  995. begin
  996. a:=t;
  997. result:=true;
  998. break;
  999. end;
  1000. end;
  1001. function Setcputype(const s:string;var a:tsettings):boolean;
  1002. var
  1003. t : tcputype;
  1004. hs : string;
  1005. begin
  1006. result:=false;
  1007. hs:=Upper(s);
  1008. for t:=low(tcputype) to high(tcputype) do
  1009. if cputypestr[t]=hs then
  1010. begin
  1011. a.cputype:=t;
  1012. result:=true;
  1013. break;
  1014. end;
  1015. {$ifdef arm}
  1016. { set default instruction set for arm }
  1017. if result then
  1018. begin
  1019. if a.cputype in [cpu_armv6m,cpu_armv6t2,cpu_armv7m] then
  1020. a.instructionset:=is_thumb
  1021. else
  1022. a.instructionset:=is_arm;
  1023. end;
  1024. {$endif arm}
  1025. end;
  1026. function SetFpuType(const s:string;var a:tfputype):boolean;
  1027. var
  1028. t : tfputype;
  1029. begin
  1030. result:=false;
  1031. for t:=low(tfputype) to high(tfputype) do
  1032. if fputypestr[t]=s then
  1033. begin
  1034. a:=t;
  1035. result:=true;
  1036. break;
  1037. end;
  1038. end;
  1039. {$if defined(arm) or defined(avr)}
  1040. function SetControllerType(const s:string;var a:tcontrollertype):boolean;
  1041. var
  1042. t : tcontrollertype;
  1043. hs : string;
  1044. begin
  1045. result:=false;
  1046. hs:=Upper(s);
  1047. for t:=low(tcontrollertype) to high(tcontrollertype) do
  1048. if embedded_controllers[t].controllertypestr=hs then
  1049. begin
  1050. a:=t;
  1051. result:=true;
  1052. break;
  1053. end;
  1054. end;
  1055. {$endif defined(arm) or defined(avr)}
  1056. function UpdateAlignmentStr(s:string;var a:talignmentinfo):boolean;
  1057. var
  1058. tok : string;
  1059. vstr : string;
  1060. l : longint;
  1061. code : integer;
  1062. b : talignmentinfo;
  1063. begin
  1064. UpdateAlignmentStr:=true;
  1065. uppervar(s);
  1066. fillchar(b,sizeof(b),0);
  1067. repeat
  1068. tok:=GetToken(s,'=');
  1069. if tok='' then
  1070. break;
  1071. vstr:=GetToken(s,',');
  1072. val(vstr,l,code);
  1073. if tok='PROC' then
  1074. b.procalign:=l
  1075. else if tok='JUMP' then
  1076. b.jumpalign:=l
  1077. else if tok='LOOP' then
  1078. b.loopalign:=l
  1079. else if tok='CONSTMIN' then
  1080. begin
  1081. b.constalignmin:=l;
  1082. if l>b.constalignmax then
  1083. b.constalignmax:=l;
  1084. end
  1085. else if tok='CONSTMAX' then
  1086. b.constalignmax:=l
  1087. else if tok='VARMIN' then
  1088. begin
  1089. b.varalignmin:=l;
  1090. if l>b.varalignmax then
  1091. b.varalignmax:=l;
  1092. end
  1093. else if tok='VARMAX' then
  1094. b.varalignmax:=l
  1095. else if tok='LOCALMIN' then
  1096. begin
  1097. b.localalignmin:=l;
  1098. if l>b.localalignmax then
  1099. b.localalignmax:=l;
  1100. end
  1101. else if tok='LOCALMAX' then
  1102. b.localalignmax:=l
  1103. else if tok='RECORDMIN' then
  1104. begin
  1105. b.recordalignmin:=l;
  1106. if l>b.recordalignmax then
  1107. b.recordalignmax:=l;
  1108. end
  1109. else if tok='RECORDMAX' then
  1110. b.recordalignmax:=l
  1111. else { Error }
  1112. UpdateAlignmentStr:=false;
  1113. until false;
  1114. Result:=Result and UpdateAlignment(a,b);
  1115. end;
  1116. function UpdateOptimizerStr(s:string;var a:toptimizerswitches):boolean;
  1117. var
  1118. tok : string;
  1119. doset,
  1120. found : boolean;
  1121. opt : toptimizerswitch;
  1122. begin
  1123. result:=true;
  1124. uppervar(s);
  1125. repeat
  1126. tok:=GetToken(s,',');
  1127. if tok='' then
  1128. break;
  1129. if Copy(tok,1,2)='NO' then
  1130. begin
  1131. delete(tok,1,2);
  1132. doset:=false;
  1133. end
  1134. else
  1135. doset:=true;
  1136. found:=false;
  1137. for opt:=low(toptimizerswitch) to high(toptimizerswitch) do
  1138. begin
  1139. if OptimizerSwitchStr[opt]=tok then
  1140. begin
  1141. found:=true;
  1142. break;
  1143. end;
  1144. end;
  1145. if found then
  1146. begin
  1147. if doset then
  1148. include(a,opt)
  1149. else
  1150. exclude(a,opt);
  1151. end
  1152. else
  1153. result:=false;
  1154. until false;
  1155. end;
  1156. function UpdateWpoStr(s: string; var a: twpoptimizerswitches): boolean;
  1157. var
  1158. tok : string;
  1159. doset,
  1160. found : boolean;
  1161. opt : twpoptimizerswitch;
  1162. begin
  1163. result:=true;
  1164. uppervar(s);
  1165. repeat
  1166. tok:=GetToken(s,',');
  1167. if tok='' then
  1168. break;
  1169. if Copy(tok,1,2)='NO' then
  1170. begin
  1171. delete(tok,1,2);
  1172. doset:=false;
  1173. end
  1174. else
  1175. doset:=true;
  1176. found:=false;
  1177. if (tok = 'ALL') then
  1178. begin
  1179. for opt:=low(twpoptimizerswitch) to high(twpoptimizerswitch) do
  1180. if doset then
  1181. include(a,opt)
  1182. else
  1183. exclude(a,opt);
  1184. end
  1185. else
  1186. begin
  1187. for opt:=low(twpoptimizerswitch) to high(twpoptimizerswitch) do
  1188. begin
  1189. if WPOptimizerSwitchStr[opt]=tok then
  1190. begin
  1191. found:=true;
  1192. break;
  1193. end;
  1194. end;
  1195. if found then
  1196. begin
  1197. if doset then
  1198. include(a,opt)
  1199. else
  1200. exclude(a,opt);
  1201. end
  1202. else
  1203. result:=false;
  1204. end;
  1205. until false;
  1206. end;
  1207. function UpdateDebugStr(s:string;var a:tdebugswitches):boolean;
  1208. var
  1209. tok : string;
  1210. doset,
  1211. found : boolean;
  1212. opt : tdebugswitch;
  1213. begin
  1214. result:=true;
  1215. uppervar(s);
  1216. repeat
  1217. tok:=GetToken(s,',');
  1218. if tok='' then
  1219. break;
  1220. if Copy(tok,1,2)='NO' then
  1221. begin
  1222. delete(tok,1,2);
  1223. doset:=false;
  1224. end
  1225. else
  1226. doset:=true;
  1227. found:=false;
  1228. for opt:=low(tdebugswitch) to high(tdebugswitch) do
  1229. begin
  1230. if DebugSwitchStr[opt]=tok then
  1231. begin
  1232. found:=true;
  1233. break;
  1234. end;
  1235. end;
  1236. if found then
  1237. begin
  1238. if doset then
  1239. include(a,opt)
  1240. else
  1241. exclude(a,opt);
  1242. end
  1243. else
  1244. result:=false;
  1245. until false;
  1246. end;
  1247. function UpdateTargetSwitchStr(s: string; var a: ttargetswitches; global: boolean): boolean;
  1248. var
  1249. tok,
  1250. value : string;
  1251. setstr: string[2];
  1252. equalspos: longint;
  1253. doset,
  1254. gotvalue,
  1255. found : boolean;
  1256. opt : ttargetswitch;
  1257. begin
  1258. result:=true;
  1259. repeat
  1260. tok:=GetToken(s,',');
  1261. if tok='' then
  1262. break;
  1263. setstr:=upper(copy(tok,length(tok),1));
  1264. if setstr='-' then
  1265. begin
  1266. setlength(tok,length(tok)-1);
  1267. doset:=false;
  1268. end
  1269. else
  1270. doset:=true;
  1271. { value specified? }
  1272. gotvalue:=false;
  1273. equalspos:=pos('=',tok);
  1274. if equalspos<>0 then
  1275. begin
  1276. value:=copy(tok,equalspos+1,length(tok));
  1277. delete(tok,equalspos,length(tok));
  1278. gotvalue:=true;
  1279. end;
  1280. found:=false;
  1281. uppervar(tok);
  1282. for opt:=low(ttargetswitch) to high(ttargetswitch) do
  1283. begin
  1284. if TargetSwitchStr[opt].name=tok then
  1285. begin
  1286. found:=true;
  1287. break;
  1288. end;
  1289. end;
  1290. if found then
  1291. begin
  1292. if not global and
  1293. TargetSwitchStr[opt].isglobal then
  1294. result:=false
  1295. else if not TargetSwitchStr[opt].hasvalue then
  1296. begin
  1297. if gotvalue then
  1298. result:=false;
  1299. if doset then
  1300. include(a,opt)
  1301. else
  1302. exclude(a,opt)
  1303. end
  1304. else
  1305. begin
  1306. if not gotvalue or
  1307. not doset then
  1308. result:=false
  1309. else
  1310. begin
  1311. case opt of
  1312. ts_auto_getter_prefix:
  1313. prop_auto_getter_prefix:=value;
  1314. ts_auto_setter_predix:
  1315. prop_auto_setter_prefix:=value;
  1316. else
  1317. begin
  1318. writeln('Internalerror 2012053001');
  1319. halt(1);
  1320. end;
  1321. end;
  1322. end;
  1323. end;
  1324. end
  1325. else
  1326. result:=false;
  1327. until false;
  1328. end;
  1329. function IncludeFeature(const s : string) : boolean;
  1330. var
  1331. i : tfeature;
  1332. begin
  1333. result:=true;
  1334. for i:=low(tfeature) to high(tfeature) do
  1335. if s=featurestr[i] then
  1336. begin
  1337. include(features,i);
  1338. exit;
  1339. end;
  1340. result:=false;
  1341. end;
  1342. function SetMinFPConstPrec(const s: string; var a: tfloattype) : boolean;
  1343. var
  1344. value, error: longint;
  1345. begin
  1346. if (upper(s)='DEFAULT') then
  1347. begin
  1348. a:=s32real;
  1349. result:=true;
  1350. exit;
  1351. end;
  1352. result:=false;
  1353. val(s,value,error);
  1354. if (error<>0) then
  1355. exit;
  1356. case value of
  1357. 32: a:=s32real;
  1358. 64: a:=s64real;
  1359. { adding support for 80 bit here is tricky, since we can't really }
  1360. { check whether the target cpu+OS actually supports it }
  1361. else
  1362. exit;
  1363. end;
  1364. result:=true;
  1365. end;
  1366. function var_align(want_align: longint): shortint;
  1367. begin
  1368. var_align := used_align(want_align,current_settings.alignment.varalignmin,current_settings.alignment.varalignmax);
  1369. end;
  1370. function var_align_size(siz: longint): shortint;
  1371. begin
  1372. siz := size_2_align(siz);
  1373. var_align_size := var_align(siz);
  1374. end;
  1375. function const_align(want_align: longint): shortint;
  1376. begin
  1377. const_align := used_align(want_align,current_settings.alignment.constalignmin,current_settings.alignment.constalignmax);
  1378. end;
  1379. function const_align_size(siz: longint): shortint;
  1380. begin
  1381. siz := size_2_align(siz);
  1382. const_align_size := const_align(siz);
  1383. end;
  1384. {$ifdef ARM}
  1385. function is_double_hilo_swapped: boolean;{$ifdef USEINLINE}inline;{$endif}
  1386. begin
  1387. result := (current_settings.fputype in [fpu_fpa,fpu_fpa10,fpu_fpa11]) and
  1388. not(cs_fp_emulation in current_settings.moduleswitches);
  1389. {$ifdef FPC_DOUBLE_HILO_SWAPPED}
  1390. { inverse result if compiler was compiled with swapped hilo already }
  1391. result := not result;
  1392. {$endif FPC_DOUBLE_HILO_SWAPPED}
  1393. end;
  1394. {$endif ARM}
  1395. function floating_point_range_check_error : boolean;
  1396. begin
  1397. result:=cs_ieee_errors in current_settings.localswitches;
  1398. end;
  1399. {****************************************************************************
  1400. Init
  1401. ****************************************************************************}
  1402. {$ifdef unix}
  1403. {$define need_path_search}
  1404. {$endif unix}
  1405. {$ifdef os2}
  1406. {$define need_path_search}
  1407. {$endif os2}
  1408. {$ifdef macos}
  1409. {$define need_path_search}
  1410. {$endif macos}
  1411. procedure get_exepath;
  1412. var
  1413. localExepath : TCmdStr;
  1414. exeName:TCmdStr;
  1415. {$ifdef need_path_search}
  1416. hs1 : TPathStr;
  1417. {$endif need_path_search}
  1418. begin
  1419. localexepath:=GetEnvironmentVariable('PPC_EXEC_PATH');
  1420. if localexepath='' then
  1421. begin
  1422. exeName := FixFileName(system.paramstr(0));
  1423. localexepath := ExtractFilePath(exeName);
  1424. end;
  1425. {$ifdef need_path_search}
  1426. if localexepath='' then
  1427. begin
  1428. hs1 := ExtractFileName(exeName);
  1429. ChangeFileExt(hs1,source_info.exeext);
  1430. {$ifdef macos}
  1431. FindFile(hs1,GetEnvironmentVariable('Commands'),false,localExepath);
  1432. {$else macos}
  1433. FindFile(hs1,GetEnvironmentVariable('PATH'),false,localExepath);
  1434. {$endif macos}
  1435. localExepath:=ExtractFilePath(localExepath);
  1436. end;
  1437. {$endif need_path_search}
  1438. exepath:=FixPath(localExepath,false);
  1439. end;
  1440. procedure DoneGlobals;
  1441. begin
  1442. librarysearchpath.Free;
  1443. unitsearchpath.Free;
  1444. objectsearchpath.Free;
  1445. includesearchpath.Free;
  1446. frameworksearchpath.Free;
  1447. LinkLibraryAliases.Free;
  1448. LinkLibraryOrder.Free;
  1449. end;
  1450. procedure InitGlobals;
  1451. begin
  1452. get_exepath;
  1453. { reset globals }
  1454. do_build:=false;
  1455. do_release:=false;
  1456. do_make:=true;
  1457. compile_level:=0;
  1458. codegenerror:=false;
  1459. DLLsource:=false;
  1460. paratarget:=system_none;
  1461. paratargetasm:=as_none;
  1462. paratargetdbg:=dbg_none;
  1463. { Output }
  1464. OutputFileName:='';
  1465. OutputPrefix:=Nil;
  1466. OutputSuffix:=Nil;
  1467. OutputExeDir:='';
  1468. OutputUnitDir:='';
  1469. { Utils directory }
  1470. utilsdirectory:='';
  1471. utilsprefix:='';
  1472. cshared:=false;
  1473. rlinkpath:='';
  1474. sysrootpath:='';
  1475. { Search Paths }
  1476. librarysearchpath:=TSearchPathList.Create;
  1477. unitsearchpath:=TSearchPathList.Create;
  1478. includesearchpath:=TSearchPathList.Create;
  1479. objectsearchpath:=TSearchPathList.Create;
  1480. frameworksearchpath:=TSearchPathList.Create;
  1481. { Def file }
  1482. usewindowapi:=false;
  1483. description:='Compiled by FPC '+version_string+' - '+target_cpu_string;
  1484. DescriptionSetExplicity:=false;
  1485. SetPEFlagsSetExplicity:=false;
  1486. SetPEOptFlagsSetExplicity:=false;
  1487. ImageBaseSetExplicity:=false;
  1488. MinStackSizeSetExplicity:=false;
  1489. MaxStackSizeSetExplicity:=false;
  1490. dllversion:='';
  1491. dllmajor:=1;
  1492. dllminor:=0;
  1493. dllrevision:=0;
  1494. nwscreenname := '';
  1495. nwthreadname := '';
  1496. nwcopyright := '';
  1497. UseDeffileForExports:=false;
  1498. UseDeffileForExportsSetExplicitly:=false;
  1499. GenerateImportSection:=false;
  1500. RelocSection:=false;
  1501. RelocSectionSetExplicitly:=false;
  1502. LinkTypeSetExplicitly:=false;
  1503. MacOSXVersionMin:='';
  1504. iPhoneOSVersionMin:='';
  1505. { memory sizes, will be overridden by parameter or default for target
  1506. in options or init_parser }
  1507. stacksize:=0;
  1508. { not initialized yet }
  1509. {$ifndef jvm}
  1510. jmp_buf_size:=-1;
  1511. {$else}
  1512. jmp_buf_size:=0;
  1513. {$endif}
  1514. apptype:=app_cui;
  1515. { Init values }
  1516. init_settings:=default_settings;
  1517. if init_settings.optimizecputype=cpu_none then
  1518. init_settings.optimizecputype:=init_settings.cputype;
  1519. LinkLibraryAliases :=TLinkStrMap.Create;
  1520. LinkLibraryOrder :=TLinkStrMap.Create;
  1521. { enable all features by default }
  1522. features:=[low(Tfeature)..high(Tfeature)];
  1523. end;
  1524. end.