globals.pas 48 KB

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