globals.pas 53 KB

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