globals.pas 51 KB

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