globals.pas 53 KB

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