globals.pas 48 KB

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