globals.pas 48 KB

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