globals.pas 49 KB

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