globals.pas 48 KB

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