globals.pas 49 KB

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