globals.pas 49 KB

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