globals.pas 52 KB

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