globals.pas 48 KB

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