globals.pas 53 KB

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