globals.pas 52 KB

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