globals.pas 54 KB

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