globals.pas 49 KB

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