globals.pas 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658
  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. {$ifdef wasm}
  490. cputype : cpu_none;
  491. optimizecputype : cpu_none;
  492. asmcputype : cpu_none;
  493. fputype : fpu_standard;
  494. {$endif wasm}
  495. {$endif not GENERIC_CPU}
  496. asmmode : asmmode_standard;
  497. {$ifndef jvm}
  498. interfacetype : it_interfacecom;
  499. {$else jvm}
  500. interfacetype : it_interfacejava;
  501. {$endif jvm}
  502. defproccall : pocall_default;
  503. sourcecodepage : 28591;
  504. minfpconstprec : s32real;
  505. disabledircache : false;
  506. tlsmodel : tlsm_none;
  507. {$if defined(i8086)}
  508. x86memorymodel : mm_small;
  509. {$endif defined(i8086)}
  510. {$if defined(ARM)}
  511. instructionset : is_arm;
  512. {$endif defined(ARM)}
  513. {$if defined(LLVM) and not defined(GENERIC_CPU)}
  514. llvmversion : llvmver_3_9;
  515. {$endif defined(LLVM) and not defined(GENERIC_CPU)}
  516. controllertype : ct_none;
  517. pmessage : nil;
  518. );
  519. var
  520. starttime : real;
  521. startsystime : TSystemTime;
  522. function getdatestr:string;
  523. function gettimestr:string;
  524. function filetimestring( t : longint) : string;
  525. function getrealtime(const st: TSystemTime) : real;
  526. function getrealtime : real;
  527. procedure DefaultReplacements(var s:ansistring);
  528. function GetEnvPChar(const envname:ansistring):pchar;
  529. procedure FreeEnvPChar(p:pchar);
  530. function is_number_float(d : double) : boolean;
  531. { discern +0.0 and -0.0 }
  532. function get_real_sign(r: bestreal): longint;
  533. procedure InitGlobals;
  534. procedure DoneGlobals;
  535. procedure register_initdone_proc(init,done:tprocedure);
  536. function string2guid(const s: string; var GUID: TGUID): boolean;
  537. function guid2string(const GUID: TGUID): string;
  538. function SetAktProcCall(const s:string; var a:tproccalloption):boolean;
  539. function Setabitype(const s:string;var a:tabi):boolean;
  540. function Setoptimizecputype(const s:string;var a:tcputype):boolean;
  541. function Setcputype(const s:string;var a:tsettings):boolean;
  542. function SetFpuType(const s:string;var a:tfputype):boolean;
  543. function SetControllerType(const s:string;var a:tcontrollertype):boolean;
  544. function HandleFeature(const s : string) : boolean;
  545. function SetMinFPConstPrec(const s: string; var a: tfloattype) : boolean;
  546. {# Routine to get the required alignment for size of data, which will
  547. be placed in bss segment, according to the current alignment requirements }
  548. function var_align(want_align: longint): shortint;
  549. function var_align_size(siz: longint): shortint;
  550. {# Routine to get the required alignment for size of data, which will
  551. be placed in data/const segment, according to the current alignment requirements }
  552. function const_align(want_align: longint): shortint;
  553. function const_align_size(siz: longint): shortint;
  554. {$ifdef ARM}
  555. function is_double_hilo_swapped: boolean;{$ifdef USEINLINE}inline;{$endif}
  556. {$endif ARM}
  557. function floating_point_range_check_error : boolean;
  558. function use_dotted_functions: boolean;
  559. { hide Sysutils.ExecuteProcess in units using this one after SysUtils}
  560. const
  561. ExecuteProcess = 'Do not use' deprecated 'Use cfileutil.RequotedExecuteProcess instead, ExecuteProcess cannot deal with single quotes as used by Unix command lines';
  562. implementation
  563. {$if defined(macos)}
  564. uses
  565. macutils;
  566. {$elseif defined(mswindows)}
  567. uses
  568. windirs;
  569. {$endif}
  570. {****************************************************************************
  571. TLinkStrMap
  572. ****************************************************************************}
  573. Constructor TLinkStrMap.create;
  574. begin
  575. inherited;
  576. itemcnt:=0;
  577. end;
  578. procedure TLinkStrMap.Add(key:ansistring;value:AnsiString='';weight:longint=LinkMapWeightDefault);
  579. begin
  580. if lookup(key)<>-1 Then
  581. exit;
  582. if itemcnt<=length(fmap) Then
  583. setlength(fmap,itemcnt+10);
  584. fmap[itemcnt].key:=key;
  585. fmap[itemcnt].value:=value;
  586. fmap[itemcnt].weight:=weight;
  587. inc(itemcnt);
  588. end;
  589. function TLinkStrMap.AddDep(keyvalue:String):boolean;
  590. var
  591. i : Longint;
  592. begin
  593. AddDep:=false;
  594. i:=pos('=',keyvalue);
  595. if i=0 then
  596. exit;
  597. Add(Copy(KeyValue,1,i-1),Copy(KeyValue,i+1,length(KeyValue)-i));
  598. AddDep:=True;
  599. end;
  600. function TLinkStrMap.AddWeight(keyvalue:String):boolean;
  601. var
  602. i,j : Longint;
  603. Code : Word;
  604. s : AnsiString;
  605. begin
  606. AddWeight:=false;
  607. i:=pos('=',keyvalue);
  608. if i=0 then
  609. exit;
  610. s:=Copy(KeyValue,i+1,length(KeyValue)-i);
  611. val(s,j,code);
  612. if code=0 Then
  613. begin
  614. Add(Copy(KeyValue,1,i-1),'',j);
  615. AddWeight:=True;
  616. end;
  617. end;
  618. procedure TLinkStrMap.addseries(keys:AnsiString;weight:longint);
  619. var
  620. i,j,k : longint;
  621. begin
  622. k:=length(keys);
  623. i:=1;
  624. while i<=k do
  625. begin
  626. j:=i;
  627. while (i<=k) and (keys[i]<>',') do
  628. inc(i);
  629. add(copy(keys,j,i-j),'',weight);
  630. inc(i);
  631. end;
  632. end;
  633. procedure TLinkStrMap.SetValue(Key:Ansistring;weight:Integer);
  634. var
  635. j : longint;
  636. begin
  637. j:=lookup(key);
  638. if j<>-1 then
  639. fmap[j].weight:=weight;
  640. end;
  641. function TLinkStrMap.find(key:Ansistring):Ansistring;
  642. var
  643. j : longint;
  644. begin
  645. find:='';
  646. j:=lookup(key);
  647. if j<>-1 then
  648. find:=fmap[j].value;
  649. end;
  650. function TLinkStrMap.lookup(key:Ansistring):longint;
  651. var
  652. i : longint;
  653. begin
  654. lookup:=-1;
  655. i:=0;
  656. while (i<itemcnt) and (fmap[i].key<>key) do
  657. inc(i);
  658. if i<>itemcnt then
  659. lookup:=i;
  660. end;
  661. procedure TLinkStrMap.SortOnWeight;
  662. var
  663. i, j : longint;
  664. m : TLinkRec;
  665. begin
  666. if itemcnt <2 then exit;
  667. for i:=0 to itemcnt-1 do
  668. for j:=i+1 to itemcnt-1 do
  669. begin
  670. if fmap[i].weight>fmap[j].weight Then
  671. begin
  672. m:=fmap[i];
  673. fmap[i]:=fmap[j];
  674. fmap[j]:=m;
  675. end;
  676. end;
  677. end;
  678. function TLinkStrMap.getlinkrec(i:longint):TLinkRec;
  679. begin
  680. result:=fmap[i];
  681. end;
  682. procedure TLinkStrMap.Expand(Src:TCmdStrList;Dest:TLinkStrMap);
  683. // expands every thing in Src to Dest for linkorder purposes.
  684. var
  685. r : longint;
  686. LibN : TCmdStr;
  687. begin
  688. while not src.empty do
  689. begin
  690. LibN:=src.getfirst;
  691. r:=lookup (LibN);
  692. if r=-1 then
  693. dest.add(LibN)
  694. else
  695. dest.addseries(fmap[r].value);
  696. end;
  697. end;
  698. procedure TLinkStrMap.UpdateWeights(Weightmap:TLinkStrMap);
  699. var
  700. l,r : longint;
  701. begin
  702. for l := 0 to itemcnt-1 do
  703. begin
  704. r:=weightmap.lookup (fmap[l].key);
  705. if r<>-1 then
  706. fmap[l].weight:=weightmap[r].weight;
  707. end;
  708. end;
  709. {****************************************************************************
  710. Time Handling
  711. ****************************************************************************}
  712. Function L0(l:longint):string;
  713. {
  714. return the string of value l, if l<10 then insert a zero, so
  715. the string is always at least 2 chars '01','02',etc
  716. }
  717. var
  718. s : string;
  719. begin
  720. Str(l,s);
  721. if l<10 then
  722. s:='0'+s;
  723. L0:=s;
  724. end;
  725. function gettimestr:string;
  726. {
  727. get the current time in a string HH:MM:SS
  728. }
  729. var
  730. st: TSystemTime;
  731. begin
  732. GetLocalTime(st);
  733. gettimestr:=L0(st.Hour)+':'+L0(st.Minute)+':'+L0(st.Second);
  734. end;
  735. function getdatestr:string;
  736. {
  737. get the current date in a string YY/MM/DD
  738. }
  739. var
  740. st: TSystemTime;
  741. begin
  742. GetLocalTime(st);
  743. getdatestr:=L0(st.Year)+'/'+L0(st.Month)+'/'+L0(st.Day);
  744. end;
  745. function filetimestring( t : longint) : string;
  746. {
  747. convert dos datetime t to a string YY/MM/DD HH:MM:SS
  748. }
  749. var
  750. DT : TDateTime;
  751. hsec : word;
  752. Year,Month,Day: Word;
  753. hour,min,sec : word;
  754. begin
  755. if t=-1 then
  756. begin
  757. Result := 'Not Found';
  758. exit;
  759. end;
  760. DT := FileDateToDateTime(t);
  761. DecodeTime(DT,hour,min,sec,hsec);
  762. DecodeDate(DT,year,month,day);
  763. Result := L0(Year)+'/'+L0(Month)+'/'+L0(Day)+' '+L0(Hour)+':'+L0(min)+':'+L0(sec);
  764. end;
  765. function getrealtime(const st: TSystemTime) : real;
  766. begin
  767. result := st.Hour*3600.0 + st.Minute*60.0 + st.Second + st.MilliSecond/1000.0;
  768. end;
  769. function getrealtime : real;
  770. var
  771. st:TSystemTime;
  772. begin
  773. GetLocalTime(st);
  774. result:=getrealtime(st);
  775. end;
  776. {****************************************************************************
  777. Default Macro Handling
  778. ****************************************************************************}
  779. procedure DefaultReplacements(var s:ansistring);
  780. {$ifdef mswindows}
  781. procedure ReplaceSpecialFolder(const MacroName: string; const ID: integer);
  782. begin
  783. // Only try to receive the special folders (and thus dynamically
  784. // load shfolder.dll) when that's needed.
  785. if pos(MacroName,s)>0 then
  786. Replace(s,MacroName,GetWindowsSpecialDir(ID));
  787. end;
  788. {$endif mswindows}
  789. {$ifdef openbsd}
  790. function GetOpenBSDLocalBase: ansistring;
  791. var
  792. envvalue: pchar;
  793. begin
  794. envvalue := GetEnvPChar('LOCALBASE');
  795. if assigned(envvalue) then
  796. Result:=envvalue
  797. else
  798. Result:='/usr/local';
  799. FreeEnvPChar(envvalue);
  800. end;
  801. function GetOpenBSDX11Base: ansistring;
  802. var
  803. envvalue: pchar;
  804. begin
  805. envvalue := GetEnvPChar('X11BASE');
  806. if assigned(envvalue) then
  807. Result:=envvalue
  808. else
  809. Result:='/usr/X11R6';
  810. FreeEnvPChar(envvalue);
  811. end;
  812. {$endif openbsd}
  813. var
  814. envstr: string;
  815. envvalue: pchar;
  816. i: integer;
  817. begin
  818. { Replace some macros }
  819. Replace(s,'$FPCVERSION',version_string);
  820. Replace(s,'$FPCFULLVERSION',full_version_string);
  821. Replace(s,'$FPCDATE',date_string);
  822. Replace(s,'$FPCCPU',target_cpu_string);
  823. Replace(s,'$FPCOS',target_os_string);
  824. if (tf_use_8_3 in Source_Info.Flags) or
  825. (tf_use_8_3 in Target_Info.Flags) then
  826. Replace(s,'$FPCTARGET',target_os_string)
  827. else
  828. Replace(s,'$FPCTARGET',target_full_string);
  829. Replace(s,'$FPCSUBARCH',lower(cputypestr[init_settings.cputype]));
  830. Replace(s,'$FPCABI',lower(abiinfo[target_info.abi].name));
  831. {$ifdef i8086}
  832. Replace(s,'$FPCMEMORYMODEL',lower(x86memorymodelstr[init_settings.x86memorymodel]));
  833. {$else i8086}
  834. Replace(s,'$FPCMEMORYMODEL','flat');
  835. {$endif i8086}
  836. {$ifdef mswindows}
  837. ReplaceSpecialFolder('$LOCAL_APPDATA',CSIDL_LOCAL_APPDATA);
  838. ReplaceSpecialFolder('$APPDATA',CSIDL_APPDATA);
  839. ReplaceSpecialFolder('$COMMON_APPDATA',CSIDL_COMMON_APPDATA);
  840. ReplaceSpecialFolder('$PERSONAL',CSIDL_PERSONAL);
  841. ReplaceSpecialFolder('$PROGRAM_FILES',CSIDL_PROGRAM_FILES);
  842. ReplaceSpecialFolder('$PROGRAM_FILES_COMMON',CSIDL_PROGRAM_FILES_COMMON);
  843. ReplaceSpecialFolder('$PROFILE',CSIDL_PROFILE);
  844. {$endif mswindows}
  845. {$ifdef openbsd}
  846. Replace(s,'$OPENBSD_LOCALBASE',GetOpenBSDLocalBase);
  847. Replace(s,'$OPENBSD_X11BASE',GetOpenBSDX11Base);
  848. {$endif openbsd}
  849. { Replace environment variables between dollar signs }
  850. i := pos('$',s);
  851. while i>0 do
  852. begin
  853. envstr:=copy(s,i+1,length(s)-i);
  854. i:=pos('$',envstr);
  855. if i>0 then
  856. begin
  857. envstr := copy(envstr,1,i-1);
  858. envvalue := GetEnvPChar(envstr);
  859. if assigned(envvalue) then
  860. begin
  861. Replace(s,'$'+envstr+'$',envvalue);
  862. // Look if there is another env.var in the string
  863. i:=pos('$',s);
  864. end
  865. else
  866. // if the env.var is not set, do not replace the env.variable
  867. // and stop looking for more env.var within the string
  868. i := 0;
  869. FreeEnvPChar(envvalue);
  870. end;
  871. end;
  872. end;
  873. {****************************************************************************
  874. OS Dependent things
  875. ****************************************************************************}
  876. function GetEnvPChar(const envname:ansistring):pchar;
  877. {$ifdef mswindows}
  878. var
  879. s : string;
  880. i,len : longint;
  881. hp,p,p2 : pchar;
  882. {$endif}
  883. begin
  884. {$ifdef hasunix}
  885. GetEnvPchar:=BaseUnix.fpGetEnv(pansichar(envname));
  886. {$define GETENVOK}
  887. {$endif}
  888. {$ifdef mswindows}
  889. GetEnvPchar:=nil;
  890. p:=GetEnvironmentStringsA;
  891. hp:=p;
  892. while hp^<>#0 do
  893. begin
  894. s:=strpas(hp);
  895. i:=pos('=',s);
  896. len:=strlen(hp);
  897. if upper(copy(s,1,i-1))=upper(envname) then
  898. begin
  899. GetMem(p2,len-length(envname));
  900. Move(hp[i],p2^,len-length(envname));
  901. GetEnvPchar:=p2;
  902. break;
  903. end;
  904. { next string entry}
  905. hp:=hp+len+1;
  906. end;
  907. FreeEnvironmentStrings(p);
  908. {$define GETENVOK}
  909. {$endif}
  910. {$ifdef os2}
  911. GetEnvPChar := Dos.GetEnvPChar (EnvName);
  912. {$define GETENVOK}
  913. {$endif}
  914. {$ifdef GETENVOK}
  915. {$undef GETENVOK}
  916. {$else}
  917. GetEnvPchar:=StrPNew(GetEnvironmentVariable(envname));
  918. if (length(GetEnvPChar)=0) then
  919. begin
  920. FreeEnvPChar(GetEnvPChar);
  921. GetEnvPChar:=nil;
  922. end;
  923. {$endif}
  924. end;
  925. procedure FreeEnvPChar(p:pchar);
  926. begin
  927. {$ifndef hasunix}
  928. {$ifndef os2}
  929. freemem(p);
  930. {$endif}
  931. {$endif}
  932. end;
  933. function is_number_float(d : double) : boolean;
  934. var
  935. bytearray : array[0..7] of byte;
  936. begin
  937. move(d,bytearray,8);
  938. { only 1.1 save, 1.0.x will use always little endian }
  939. {$ifdef FPC_BIG_ENDIAN}
  940. result:=((bytearray[0] and $7f)<>$7f) or ((bytearray[1] and $f0)<>$f0);
  941. {$else FPC_BIG_ENDIAN}
  942. result:=((bytearray[7] and $7f)<>$7f) or ((bytearray[6] and $f0)<>$f0);
  943. {$endif FPC_BIG_ENDIAN}
  944. end;
  945. function get_real_sign(r: bestreal): longint;
  946. var
  947. p: pbyte;
  948. begin
  949. p := pbyte(@r);
  950. {$ifdef CPU_ARM}
  951. inc(p,4);
  952. {$else}
  953. {$ifdef FPC_LITTLE_ENDIAN}
  954. inc(p,sizeof(r)-1);
  955. {$endif}
  956. {$endif}
  957. if (p^ and $80) = 0 then
  958. result := 1
  959. else
  960. result := -1;
  961. end;
  962. function convertdoublerec(d : tcompdoublerec) : tcompdoublerec;{$ifdef USEINLINE}inline;{$endif}
  963. {$ifdef CPUARM}
  964. var
  965. i : longint;
  966. begin
  967. for i:=0 to 3 do
  968. begin
  969. result.bytes[i+4]:=d.bytes[i];
  970. result.bytes[i]:=d.bytes[i+4];
  971. end;
  972. {$else CPUARM}
  973. begin
  974. result:=d;
  975. {$endif CPUARM}
  976. end;
  977. { '('D1:'00000000-'D2:'0000-'D3:'0000-'D4:'0000-000000000000)' }
  978. function string2guid(const s: string; var GUID: TGUID): boolean;
  979. function ishexstr(const hs: string): boolean;
  980. var
  981. i: integer;
  982. begin
  983. ishexstr:=false;
  984. for i:=1 to Length(hs) do begin
  985. if not (hs[i] in ['0'..'9','A'..'F','a'..'f']) then
  986. exit;
  987. end;
  988. ishexstr:=true;
  989. end;
  990. function hexstr2longint(const hexs: string): longint;
  991. var
  992. i: integer;
  993. rl: longint;
  994. begin
  995. rl:=0;
  996. for i:=1 to length(hexs) do begin
  997. rl:=rl shl 4;
  998. case hexs[i] of
  999. '0'..'9' : inc(rl,ord(hexs[i])-ord('0'));
  1000. 'A'..'F' : inc(rl,ord(hexs[i])-ord('A')+10);
  1001. 'a'..'f' : inc(rl,ord(hexs[i])-ord('a')+10);
  1002. end
  1003. end;
  1004. hexstr2longint:=rl;
  1005. end;
  1006. var
  1007. i: integer;
  1008. begin
  1009. if (Length(s)=38) and (s[1]='{') and (s[38]='}') and
  1010. (s[10]='-') and (s[15]='-') and (s[20]='-') and (s[25]='-') and
  1011. ishexstr(copy(s,2,8)) and ishexstr(copy(s,11,4)) and
  1012. ishexstr(copy(s,16,4)) and ishexstr(copy(s,21,4)) and
  1013. ishexstr(copy(s,26,12)) then begin
  1014. GUID.D1:=dword(hexstr2longint(copy(s,2,8)));
  1015. { these values are arealdy in the correct range (4 chars = word) }
  1016. GUID.D2:=word(hexstr2longint(copy(s,11,4)));
  1017. GUID.D3:=word(hexstr2longint(copy(s,16,4)));
  1018. for i:=0 to 1 do
  1019. GUID.D4[i]:=byte(hexstr2longint(copy(s,21+i*2,2)));
  1020. for i:=2 to 7 do
  1021. GUID.D4[i]:=byte(hexstr2longint(copy(s,22+i*2,2)));
  1022. string2guid:=true;
  1023. end
  1024. else if (length(s)=0) then
  1025. begin
  1026. FillChar(GUID,SizeOf(GUID),0);
  1027. string2guid:=true;
  1028. end
  1029. else
  1030. string2guid:=false;
  1031. end;
  1032. function guid2string(const GUID: TGUID): string;
  1033. begin
  1034. guid2string:=
  1035. '{'+hexstr(GUID.D1,8)+
  1036. '-'+hexstr(GUID.D2,4)+
  1037. '-'+hexstr(GUID.D3,4)+
  1038. '-'+hexstr(GUID.D4[0],2)+hexstr(GUID.D4[1],2)+
  1039. '-'+hexstr(GUID.D4[2],2)+hexstr(GUID.D4[3],2)+
  1040. hexstr(GUID.D4[4],2)+hexstr(GUID.D4[5],2)+
  1041. hexstr(GUID.D4[6],2)+hexstr(GUID.D4[7],2)+
  1042. '}';
  1043. end;
  1044. function SetAktProcCall(const s:string; var a:tproccalloption):boolean;
  1045. const
  1046. DefProcCallName : array[tproccalloption] of string[16] = ('',
  1047. 'CDECL',
  1048. 'CPPDECL',
  1049. 'FAR16',
  1050. 'OLDFPCCALL',
  1051. '', { internproc }
  1052. '', { syscall }
  1053. 'PASCAL',
  1054. 'REGISTER',
  1055. 'SAFECALL',
  1056. 'STDCALL',
  1057. 'SOFTFLOAT',
  1058. 'MWPASCAL',
  1059. 'INTERRUPT',
  1060. 'HARDFLOAT',
  1061. 'SYSV_ABI_DEFAULT',
  1062. 'SYSV_ABI_CDECL',
  1063. 'MS_ABI_DEFAULT',
  1064. 'MS_ABI_CDECL',
  1065. 'VECTORCALL'
  1066. );
  1067. var
  1068. t : tproccalloption;
  1069. hs : string;
  1070. begin
  1071. result:=false;
  1072. if (s = '') then
  1073. exit;
  1074. hs:=upper(s);
  1075. if (hs = 'DEFAULT') then
  1076. begin
  1077. a := pocall_default;
  1078. result := true;
  1079. exit;
  1080. end;
  1081. for t:=low(tproccalloption) to high(tproccalloption) do
  1082. if DefProcCallName[t]=hs then
  1083. begin
  1084. a:=t;
  1085. result:=true;
  1086. break;
  1087. end;
  1088. end;
  1089. function Setabitype(const s:string;var a:tabi):boolean;
  1090. var
  1091. t : tabi;
  1092. hs : string;
  1093. begin
  1094. result:=false;
  1095. hs:=Upper(s);
  1096. for t:=low(t) to high(t) do
  1097. if abiinfo[t].supported and
  1098. (abiinfo[t].name=hs) then
  1099. begin
  1100. a:=t;
  1101. { abi_old_win32_gnu is a win32 i386 specific "feature" }
  1102. if (t<>abi_old_win32_gnu) or (target_info.system=system_i386_win32) then
  1103. result:=true;
  1104. break;
  1105. end;
  1106. end;
  1107. function Setoptimizecputype(const s:string;var a:tcputype):boolean;
  1108. var
  1109. t : tcputype;
  1110. hs : string;
  1111. begin
  1112. result:=false;
  1113. hs:=Upper(s);
  1114. for t:=low(tcputype) to high(tcputype) do
  1115. if cputypestr[t]=hs then
  1116. begin
  1117. a:=t;
  1118. result:=true;
  1119. break;
  1120. end;
  1121. end;
  1122. function Setcputype(const s:string;var a:tsettings):boolean;
  1123. var
  1124. t : tcputype;
  1125. hs : string;
  1126. begin
  1127. result:=false;
  1128. hs:=Upper(s);
  1129. for t:=low(tcputype) to high(tcputype) do
  1130. if cputypestr[t]=hs then
  1131. begin
  1132. a.cputype:=t;
  1133. result:=true;
  1134. break;
  1135. end;
  1136. {$ifdef arm}
  1137. { set default instruction set for arm }
  1138. if result then
  1139. begin
  1140. if a.cputype in [cpu_armv6m,cpu_armv6t2,cpu_armv7m,cpu_armv7em] then
  1141. a.instructionset:=is_thumb
  1142. else
  1143. a.instructionset:=is_arm;
  1144. end;
  1145. {$endif arm}
  1146. end;
  1147. function SetFpuType(const s:string;var a:tfputype):boolean;
  1148. var
  1149. t : tfputype;
  1150. begin
  1151. result:=false;
  1152. for t:=low(tfputype) to high(tfputype) do
  1153. if fputypestr[t]=s then
  1154. begin
  1155. a:=t;
  1156. result:=true;
  1157. break;
  1158. end;
  1159. end;
  1160. function SetControllerType(const s:string;var a:tcontrollertype):boolean;
  1161. var
  1162. t : tcontrollertype;
  1163. hs : string;
  1164. begin
  1165. { The following check allows to reduce amount of code for platforms }
  1166. { not supporting microcontrollers due to evaluation at compile time. }
  1167. {$PUSH}
  1168. {$WARN 6018 OFF} (* Unreachable code due to compile time evaluation *)
  1169. if ControllerSupport then
  1170. begin
  1171. result:=false;
  1172. hs:=Upper(s);
  1173. for t:=low(tcontrollertype) to high(tcontrollertype) do
  1174. if embedded_controllers[t].controllertypestr=hs then
  1175. begin
  1176. a:=t;
  1177. result:=true;
  1178. break;
  1179. end;
  1180. end
  1181. else
  1182. begin
  1183. a := ct_none;
  1184. Result := true;
  1185. end;
  1186. {$POP}
  1187. end;
  1188. function HandleFeature(const s : string) : boolean;
  1189. var
  1190. i : tfeature;
  1191. begin
  1192. result:=true;
  1193. for i:=low(tfeature) to high(tfeature) do
  1194. if s=featurestr[i] then
  1195. begin
  1196. include(features,i);
  1197. exit;
  1198. end;
  1199. { Also support -Sfnoheap to exclude heap }
  1200. if Copy(S,1,2)='NO' then
  1201. for i:=low(tfeature) to high(tfeature) do
  1202. if s='NO'+featurestr[i] then
  1203. begin
  1204. exclude(features,i);
  1205. exit;
  1206. end;
  1207. result:=false;
  1208. end;
  1209. function SetMinFPConstPrec(const s: string; var a: tfloattype) : boolean;
  1210. var
  1211. value, error: longint;
  1212. begin
  1213. if (upper(s)='DEFAULT') then
  1214. begin
  1215. a:=s32real;
  1216. result:=true;
  1217. exit;
  1218. end;
  1219. result:=false;
  1220. val(s,value,error);
  1221. if (error<>0) then
  1222. exit;
  1223. case value of
  1224. 32: a:=s32real;
  1225. 64: a:=s64real;
  1226. { adding support for 80 bit here is tricky, since we can't really }
  1227. { check whether the target cpu+OS actually supports it }
  1228. else
  1229. exit;
  1230. end;
  1231. result:=true;
  1232. end;
  1233. function var_align(want_align: longint): shortint;
  1234. begin
  1235. var_align := used_align(want_align,current_settings.alignment.varalignmin,current_settings.alignment.varalignmax);
  1236. end;
  1237. function var_align_size(siz: longint): shortint;
  1238. begin
  1239. siz := size_2_align(siz);
  1240. var_align_size := var_align(siz);
  1241. end;
  1242. function const_align(want_align: longint): shortint;
  1243. begin
  1244. const_align := used_align(want_align,current_settings.alignment.constalignmin,current_settings.alignment.constalignmax);
  1245. end;
  1246. function const_align_size(siz: longint): shortint;
  1247. begin
  1248. siz := size_2_align(siz);
  1249. const_align_size := const_align(siz);
  1250. end;
  1251. {$ifdef ARM}
  1252. function is_double_hilo_swapped: boolean;{$ifdef USEINLINE}inline;{$endif}
  1253. begin
  1254. result := (current_settings.fputype in [fpu_fpa,fpu_fpa10,fpu_fpa11]) and
  1255. not(cs_fp_emulation in current_settings.moduleswitches);
  1256. {$ifdef FPC_DOUBLE_HILO_SWAPPED}
  1257. { inverse result if compiler was compiled with swapped hilo already }
  1258. result := not result;
  1259. {$endif FPC_DOUBLE_HILO_SWAPPED}
  1260. end;
  1261. {$endif ARM}
  1262. function floating_point_range_check_error : boolean;
  1263. begin
  1264. result:=cs_ieee_errors in current_settings.localswitches;
  1265. end;
  1266. function use_dotted_functions: boolean;
  1267. begin
  1268. result:=
  1269. (target_info.system in systems_dotted_function_names) and
  1270. (target_info.abi<>abi_powerpc_elfv2);
  1271. end;
  1272. {****************************************************************************
  1273. Init
  1274. ****************************************************************************}
  1275. {$ifdef unix}
  1276. {$define need_path_search}
  1277. {$endif unix}
  1278. {$ifdef os2}
  1279. {$define need_path_search}
  1280. {$endif os2}
  1281. {$ifdef macos}
  1282. {$define need_path_search}
  1283. {$endif macos}
  1284. procedure get_exepath;
  1285. var
  1286. localExepath : TCmdStr;
  1287. exeName:TCmdStr;
  1288. {$ifdef need_path_search}
  1289. hs1 : TPathStr;
  1290. {$endif need_path_search}
  1291. begin
  1292. localexepath:=GetEnvironmentVariable('PPC_EXEC_PATH');
  1293. exeName := '';
  1294. if localexepath='' then
  1295. begin
  1296. exeName := FixFileName(system.paramstr(0));
  1297. localexepath := ExtractFilePath(exeName);
  1298. end;
  1299. {$ifdef need_path_search}
  1300. if localexepath='' then
  1301. begin
  1302. hs1 := ExtractFileName(exeName);
  1303. hs1 := ChangeFileExt(hs1,source_info.exeext);
  1304. {$ifdef macos}
  1305. FindFile(hs1,GetEnvironmentVariable('Commands'),false,localExepath);
  1306. {$else macos}
  1307. FindFile(hs1,GetEnvironmentVariable('PATH'),false,localExepath);
  1308. {$endif macos}
  1309. localExepath:=ExtractFilePath(localExepath);
  1310. end;
  1311. {$endif need_path_search}
  1312. exepath:=FixPath(localExepath,false);
  1313. end;
  1314. type
  1315. tinitdoneentry=record
  1316. init:tprocedure;
  1317. done:tprocedure;
  1318. end;
  1319. pinitdoneentry=^tinitdoneentry;
  1320. const
  1321. initdoneprocs : TFPList = nil;
  1322. procedure allocinitdoneprocs;
  1323. begin
  1324. { Avoid double initialization }
  1325. if assigned(initdoneprocs) then
  1326. exit;
  1327. initdoneprocs:=tfplist.create;
  1328. end;
  1329. procedure register_initdone_proc(init,done:tprocedure);
  1330. var
  1331. entry : pinitdoneentry;
  1332. begin
  1333. new(entry);
  1334. entry^.init:=init;
  1335. entry^.done:=done;
  1336. { Do not rely on the fact that
  1337. globals unit initialization code
  1338. has already been executed.
  1339. Unit initialization order is too
  1340. uncertian for that. PM }
  1341. if not assigned(initdoneprocs) then
  1342. allocinitdoneprocs;
  1343. initdoneprocs.add(entry);
  1344. end;
  1345. procedure callinitprocs;
  1346. var
  1347. i : longint;
  1348. begin
  1349. if not assigned(initdoneprocs) then
  1350. exit;
  1351. for i:=0 to initdoneprocs.count-1 do
  1352. with pinitdoneentry(initdoneprocs[i])^ do
  1353. if assigned(init) then
  1354. init();
  1355. end;
  1356. procedure calldoneprocs;
  1357. var
  1358. i : longint;
  1359. begin
  1360. if not assigned(initdoneprocs) then
  1361. exit;
  1362. for i:=0 to initdoneprocs.count-1 do
  1363. with pinitdoneentry(initdoneprocs[i])^ do
  1364. if assigned(done) then
  1365. done();
  1366. end;
  1367. procedure freeinitdoneprocs;
  1368. var
  1369. i : longint;
  1370. begin
  1371. if not assigned(initdoneprocs) then
  1372. exit;
  1373. for i:=0 to initdoneprocs.count-1 do
  1374. dispose(pinitdoneentry(initdoneprocs[i]));
  1375. initdoneprocs.free;
  1376. { Reset variable, to be on the safe side }
  1377. initdoneprocs:=nil;
  1378. end;
  1379. procedure DoneGlobals;
  1380. begin
  1381. calldoneprocs;
  1382. librarysearchpath.Free;
  1383. unitsearchpath.Free;
  1384. objectsearchpath.Free;
  1385. includesearchpath.Free;
  1386. frameworksearchpath.Free;
  1387. LinkLibraryAliases.Free;
  1388. LinkLibraryOrder.Free;
  1389. packagesearchpath.Free;
  1390. namespacelist.Free;
  1391. end;
  1392. procedure InitGlobals;
  1393. begin
  1394. get_exepath;
  1395. { reset globals }
  1396. do_build:=false;
  1397. do_release:=false;
  1398. do_make:=true;
  1399. compile_level:=0;
  1400. codegenerror:=false;
  1401. { Output }
  1402. OutputFileName:='';
  1403. OutputPrefix:=Nil;
  1404. OutputSuffix:=Nil;
  1405. OutputExeDir:='';
  1406. OutputUnitDir:='';
  1407. { Utils directory }
  1408. utilsdirectory:='';
  1409. utilsprefix:='';
  1410. cshared:=false;
  1411. rlinkpath:='';
  1412. sysrootpath:='';
  1413. { Search Paths }
  1414. unicodepath:='';
  1415. librarysearchpath:=TSearchPathList.Create;
  1416. unitsearchpath:=TSearchPathList.Create;
  1417. includesearchpath:=TSearchPathList.Create;
  1418. objectsearchpath:=TSearchPathList.Create;
  1419. frameworksearchpath:=TSearchPathList.Create;
  1420. packagesearchpath:=TSearchPathList.Create;
  1421. namespacelist:=TCmdStrList.Create;
  1422. { Def file }
  1423. usewindowapi:=false;
  1424. description:='Compiled by FPC '+version_string+' - '+target_cpu_string;
  1425. DescriptionSetExplicity:=false;
  1426. SetPEFlagsSetExplicity:=false;
  1427. SetPEOptFlagsSetExplicity:=false;
  1428. SetPEOSVersionSetExplicitely:=false;
  1429. SetPESubSysVersionSetExplicitely:=false;
  1430. SetPEUserVersionSetExplicitely:=false;
  1431. ImageBaseSetExplicity:=false;
  1432. MinStackSizeSetExplicity:=false;
  1433. MaxStackSizeSetExplicity:=false;
  1434. dllversion:='';
  1435. dllmajor:=1;
  1436. dllminor:=0;
  1437. dllrevision:=0;
  1438. nwscreenname := '';
  1439. nwthreadname := '';
  1440. nwcopyright := '';
  1441. UseDeffileForExports:=false;
  1442. UseDeffileForExportsSetExplicitly:=false;
  1443. GenerateImportSection:=false;
  1444. RelocSection:=false;
  1445. RelocSectionSetExplicitly:=false;
  1446. MacOSXVersionMin:='';
  1447. iPhoneOSVersionMin:='';
  1448. { memory sizes, will be overridden by parameter or default for target
  1449. in options or init_parser }
  1450. stacksize:=0;
  1451. { not initialized yet }
  1452. apptype:=app_cui;
  1453. { Init values }
  1454. init_settings:=default_settings;
  1455. if init_settings.optimizecputype=cpu_none then
  1456. init_settings.optimizecputype:=init_settings.cputype;
  1457. LinkLibraryAliases :=TLinkStrMap.Create;
  1458. LinkLibraryOrder :=TLinkStrMap.Create;
  1459. { enable all features by default }
  1460. features:=[low(Tfeature)..high(Tfeature)];
  1461. callinitprocs;
  1462. end;
  1463. initialization
  1464. allocinitdoneprocs;
  1465. finalization
  1466. freeinitdoneprocs;
  1467. end.