globals.pas 47 KB

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