globals.pas 49 KB

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