globals.pas 48 KB

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