globals.pas 48 KB

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