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