globals.pas 51 KB

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